-1
jQuery.ajax({
    async: false,
    url: url,
    dataType: dataType,
    crossDomain: false
}).done(function(data) {
    callback(null, data);
}).fail(function(jqXHR, textStatus, errorThrown) {
    if (jqXHR.statusText === 'OK') {
        console.log('Unable to load resource:', url, 'error:', errorThrown);
    }
    callback(errorThrown)
});

For the above ajax request call i am facing some errors

Error 1: Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers in preflight

Error 2: DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load

  • so is the client making the call to the server? in a quick google search for the first error pulled up this stack overflow question which might relatate – GoldenWest May 01 '17 at 17:21
  • http://stackoverflow.com/questions/25727306/request-header-field-access-control-allow-headers-is-not-allowed-by-access-contr – GoldenWest May 01 '17 at 17:21

3 Answers3

0

The server you're trying to hit is preventing the browser from completing the request via its HTTP access control policy (CORS). Specifically the "Access-Control-Allow-Headers".

You need to either:

  • Configure the server to allow "X-Requested-With" header, or:
  • Remove the "X-Requested-With" from jQuery's AJAX call
lxe
  • 7,051
  • 2
  • 20
  • 32
0
  1. Avoid synchronous requests. They are deprecated. Read this article on MDN.

  2. You are facing the same-origin policy implemented by all web browsers. Since CORS is apparently not available for you, you should rely on JSONP.

  3. If you want to load a script, it would be better to use jQuery.getScript().

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
0

I found the root cause when we make an ajax call to the js file with dataType = 'script'

when we mention dataType = 'script' it will evals the script data and returns the data inside the success callback. suppose if the js file is having any error during eval(js) it will find the error and it will returns undefined inside the success callback.