jQuery POST not sending Content-Type on IE10

2019-06-05 13:43发布

问题:

I'm using jQuery 1.9.1 and Internet Explorer Version 10.0.9200.16686, Update Version 10.0.9 (KB2870699), on Windows 7 64-bit.

When using $.ajax to POST, the Content-Type header is not being sent. It works fine in Chrome and other browsers, and I believe used to work fine before on IE10 so I suspect some update to IE10 caused an issue.

I can also manually create an XMLHttpRequest and send a request, so I'm assuming it must be a bug in jQuery that's the real issue? I tried upgrading to 1.10.2, but get the same problem.

I have the contentType set using $.ajaxSetup:

$.ajaxSetup({
    contentType: 'application/json; charset=utf-8',
    context: document.body,
    dataType: 'json',
    accepts: 'application/json',
    processdata: true,
    cache: false,
    crossDomain: true,
    xhrFields: { withCredentials: true }
});

I've also tried explicitly setting it as part of the $.ajax options.

var options = {
    url: basePath + 'login',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: JSON.stringify({
        UserName: username,
        Password: password,
        RememberMe: rememberMe || false
    })
};
return $.ajax(options).then(...);

Any ideas how to fix it will be appreciated.

Note: we were initially using CORS, hence the reason for the xhrFields and crossDomain settings, but are currently hosting everything on the same domain because of issues with IE8 and IE9 and XDomainRequest.

回答1:

It appears that we still had the xdr.js script included to enable CORS on IE, which was causing this issue.