javascript - jQuery Ajax 请求异步 false

标签 javascript jquery ajax

我想知道如果我发送这样的 ajax 请求,为什么会出现此错误:

$.ajax({
    url: "testOperation.php",
    async: false,
    data: compareHeure,
    success: function (data, statusRequest) {
})

错误:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

有问题吗?

谢谢,

最佳答案

我想知道为什么会出现此错误

简短的回答是因为 ajax 中的这个选项:

async: false,

对于您当前遇到的问题,可能还有其他更好的解决方案。如果您发布有关为什么需要使用同步 ajax 的更多详细信息?


要么这样使用:

$.ajax({
   url:'',
   type:'post',
   success:function(data){
       // make another ajax call here then    
   }
})

你可以使用jQuery.when():

$.when( $.ajax("/page1.php"), $.ajax("/page2.php") ).done(function(a1, a2) {
    // here you can get both ajax response when both gets completed.
    console.log(a1);
    console.log(a2);
});

此处 .done() 仅在两个 ajax 都完成时执行。

关于javascript - jQuery Ajax 请求异步 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37938682/

相关文章:

javascript - 同位素 - 使用 URL 哈希进行过滤

Javascript 不适用于 : onclick show hidden div from textarea

java - .innerHTML 未定义

jquery - 如何在单页中使用 jquery 滚动到保持 anchor 事件属性

javascript - 如何防止在 Chrome 中发送 Origin HTTP header ?

javascript - 发送 jsonp 请求phonegap 时捕获响应

javascript - 加载资源失败:net::ERR_CONTENT_LENGTH_MISMATCH

javascript - 谷歌地图 v3 : How can I reliably I retrieve Icon size and anchor position?

javascript - 正则表达式在多个尾随 ]]]] 的情况下无法正常工作

javascript - 如何使用 jQuery 和 Ajax 在下拉列表中插入默认项并禁用它