我试图进行 ajax 调用,将 async 属性设置为 false,但在 chrome 和 ie 上运行时出现错误。
我知道由于用户体验问题,Firefox 强烈推荐异步 ajax 调用,但我需要在继续做其他事情之前获得响应,并且可以(并且强制)等待收到响应。
这是工作和不工作的代码,任何帮助将不胜感激。
data = $.ajax({
url: Common.serverPath + 'recovery/list',
crossDomain: true,
dataType: 'json',
async: true,
type: 'GET',
contentType: 'application/json',
xhrFields: {
withCredentials: true
}
});
setTimeout(function(){ console.log(data.statusText);
},3000);
控制台:确定
data = $.ajax({
url: Common.serverPath + 'recovery/list',
crossDomain: true,
dataType: 'json',
async: false,
type: 'GET',
contentType: 'application/json',
xhrFields: {
withCredentials: true
}
});
setTimeout(function(){ console.log(data.statusText); },
3000);
控制台:InvalidAccessError:底层对象不支持参数或操作
chrome 和 ie 都可以正常工作
最佳答案
我发现“跨域请求和dataType:“jsonp”请求不支持同步操作。”即使它在 ie 和 chrome 中也是如此:)
关于javascript - 使用 jQuery 的同步 ajax 调用在 firefox 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33231978/