我一直在尝试让 ajax 调用在 IE9 中正常工作。我可以在网络数据中看到请求顺利通过。jQuery $.ajax() 调用返回的 jqXHR 对象包含响应数据。然而,我的成功/错误/完成回调没有触发。这是代码...
在我的脚本顶部:
// override xhr for browser that use XDR
if ('XDomainRequest' in window && window.XDomainRequest !== null) {
// override default jQuery transport for IE
jQuery.ajaxSettings.xhr = function() {
try { return new XDomainRequest(); }
catch(e) {
console.log('test');
}
};
// Maybe a 304 response is causing the callbacks not to fire?
// This makes sure I'm getting 200
jQuery.ajaxSettings.cache = false;
// also, override the support check
jQuery.support.cors = true;
}
然后我的ajax调用......很简单。
$.ajax({
url: url,
success: success,
complete: complete,
beforeSend: beforeSend,
error: error
});
function success(response, textStatus, jqXHR){
console.log('success');
if( typeof fn == 'function'){
fn(response.data);
} else {
console.log('Invalid callback supplied for dataGateway');
}
}
function error(jqXHR, textStatus, errorThrown){
console.log("Could not retrieve data from API.");
return;
}
function complete(jqXHR, textStatus){
console.log('complete');
}
// This is the only callback that gets fired
function beforeSend(jqXHR, settings){
console.log('before send');
}
有谁知道如何解决这个问题?