Making AJAX() call result into Cross-Origin Request Blocked error
Asked Answered
C

0

2

I was trying to send a form details to a folder containing the php script to process and validated it using AJAX() method ,

But I am getting "Cross-Origin Request Blocked" error . I know this is cross browser problem but I am not able to find out a solution for this .

  1. Writing a Proxy server is one option . Can someone help me out with this I am very new to all this .

here is my code

<script>
    *<![CDATA[*/
    $(document).ready(function(){       
        $("#abusoForm #enviar").livequery("click", function(e){ 
            e.preventDefault();
            console.log("Click is working");
            var hidden = $('#mensaje').val();
            var category = $('#opcmarcar').val();
            var name = $('#nombre').val();
            var phone = $('#telefono').val();
            var mail = $('#email').val();
            var cf_mail = $('#confirma_email').val();
            var k = ""
            var z = "anotherdomain.com";
            var otro = $('#otro_email').val();
            var E = $("#abusoForm #enviar").val();

            //Ajax call happening here 
            var vajx = $.ajax({
                url: z,
                type: "POST",
                data: { 
                    'h': hidden, 
                    'c': category,
                    'n': name,
                    'p': phone ,
                    'm': mail,
                    'cm': cf_mail,
                    'otro1': otro,
                    "enviar": E,
                    async: false
                }
            }).responseText;

            //Now I have to use the variable vajx to post a message about the submition of the form ;
            $('#resultip').html(vajx);
        })
    });
    /*]]>*/
</script>

I read this article http://www.html5rocks.com/en/tutorials/cors/ but cant understand much

. Another possiblity would be to use CORS but I am not getting it how to use the script to use with JQUERY function AJAX().

Consequent answered 27/5, 2014 at 7:16 Comment(13)
Assuming that the url contained in z is outside of your control, writing a server-side proxy is your only option.Tropine
In short: you can't fetch content from an external URL using AJAX alone. Use a PHP script that resides on your server to do the fetching and then send the requests to this PHP script to have the data transferred to your client-side code.Buseck
Yes, you can make a call to PHP script on your server and have that php script call the remote url through CURL.Inelastic
Its possible if server side code support jsonpBathilda
Do you have control of the server? If so you can set its CORS headers, its simple!Glaucescent
@NickM How to see the CORS?Consequent
Sorry I meant "set", not "see". Do you control the server where you PHP script lies?Glaucescent
@NickM yes I know the file where its located I can modify it .Consequent
But can you modify the server's apache config files?Glaucescent
@NickM no bro I donot have any access to itConsequent
@RoryMcCrossan can you give me some example how to write a proxy in php ?Consequent
Let us continue this discussion in chat.Consequent
@VikramAnandBhushan unfortunately you cannot use my solution as setting up CORS headers on a server requires access to the apache configs.Glaucescent

© 2022 - 2024 — McMap. All rights reserved.