0

When I add header HTTP_X_REQUESTED_WITH in ajax for request to another server then it give erorr as Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.xxxxxxxxxxxx.com/checkurl.php?action=xxxxxxx. This can be fixed by moving the resource to the same domain or enabling CORS.

and if I remove this header it working properly.

I have many ajax request so I use this format to add header in all ajax request

 $(document).ajaxSend(function (event, jqxhr, settings) {
     jqxhr.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest')
 });

and my ajax is :

 $.ajax({
    url:sitePath+'Xxxxxxxxx/checkurl.php?action=Pages&page='+actionPage,
    type :'POST',
    crossDomain:true,
    success : function(data){
        hideLoadingDiv();
        if(data.getElementsByTagName("message")[0].getElementsByTagName("messageType")[0].childNodes[0].nodeValue=="SUCCESS")
        {
            document.getElementById(divID).innerHTML=data.getElementsByTagName("PageBody")[0].childNodes[0].nodeValue+'<span><a onclick="return checkRegistrationValidation();" href="javascript:void(0);" class="orengeBtn">Back</a></span>';
            displayDiv(divID);
        }
        else if(data.getElementsByTagName("message")[0].getElementsByTagName("messageType")[0].childNodes[0].nodeValue=="ERROR")
        {
            showErrorMessage(data.getElementsByTagName("message")[0].getElementsByTagName("messageText")[0].childNodes[0].nodeValue);
        }
        else
        {
            generalError();
        }
    },
    error:function(xhr,ajaxOptions, thrownError){
        hideLoadingDiv();
        if(xhr.status==200){
            generalError();
        }
        else{
            networkError();
        }
        if(debugMode==1){
            displayAjaxError(xhr,thrownError);
        }
    }
});

and in my server file I use

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With, Content-Type')

but it give error every time when I add header. And after remove working properly. Please help.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Jitendra
  • 558
  • 8
  • 23

1 Answers1

0

You can find answer here:

Cross-Domain AJAX doesn't send X-Requested-With header

You have to either add the headers manually in your ajax:

$.ajax({
    url:sitePath+'Xxxxxxxxx/checkurl.php?action=Pages&page='+actionPage,
    type :'POST',
    crossDomain:true,
    headers: {'X-Requested-With': 'XMLHttpRequest'}

or make the request not crossdomain:

$.ajax({
    url:sitePath+'Xxxxxxxxx/checkurl.php?action=Pages&page='+actionPage,
    type :'POST',
    crossDomain:false,
Community
  • 1
  • 1
VMAtm
  • 27,943
  • 17
  • 79
  • 125