jquery ajax 异步调用:真与 async:false
jquery ajax call async: true vs async:false
在我的 chrome 扩展中,我使用了 jquery 库,这是我的 post ajax 请求,但我很困惑它是同步请求还是异步请求?
$.ajax({
type: 'POST',
url: "https://api.xxx",
data: { domain: b },
cache: false,
success: function (data)
{
var response = JSON.parse(data);
.......
if ()
{
}
else
{
}
},
error: function (data)
{
$('.popup').html("test");
}
});
return $ret;
}
如果是同步的话加
就够了
async:true
使其异步?
注意:我有另一个 GET 请求。事情是 POST 请求工作得很好,但在获取请求期间响应真的很慢,有时我不得不多次点击或重新加载才能使其工作。
Jquery Ajax 调用本身是异步的。请参考:http://api.jquery.com/jquery.ajax/
async (default: true
)
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active
因此,您无需执行任何操作即可使其异步。此外,无论如何,让这些调用同步通常不是一个好主意。
在我的 chrome 扩展中,我使用了 jquery 库,这是我的 post ajax 请求,但我很困惑它是同步请求还是异步请求?
$.ajax({
type: 'POST',
url: "https://api.xxx",
data: { domain: b },
cache: false,
success: function (data)
{
var response = JSON.parse(data);
.......
if ()
{
}
else
{
}
},
error: function (data)
{
$('.popup').html("test");
}
});
return $ret;
}
如果是同步的话加
就够了async:true
使其异步?
注意:我有另一个 GET 请求。事情是 POST 请求工作得很好,但在获取请求期间响应真的很慢,有时我不得不多次点击或重新加载才能使其工作。
Jquery Ajax 调用本身是异步的。请参考:http://api.jquery.com/jquery.ajax/
async (default:
true
)
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active
因此,您无需执行任何操作即可使其异步。此外,无论如何,让这些调用同步通常不是一个好主意。