$.ajax({
url: "test.html",
error: function(){
//do something
},
success: function(){
//do something
}
});
有时成功函数很好,有时不行。
如何为这个ajax请求设置超时?例如,3秒,如果时间超时,则显示错误。
问题是,ajax请求冻结块直到完成。如果服务器宕机一段时间,它将永远不会结束。
$.ajax({
url: "test.html",
error: function(){
//do something
},
success: function(){
//do something
}
});
有时成功函数很好,有时不行。
如何为这个ajax请求设置超时?例如,3秒,如果时间超时,则显示错误。
问题是,ajax请求冻结块直到完成。如果服务器宕机一段时间,它将永远不会结束。
下面是一些示例,演示如何在jQuery的新旧范例中设置和检测超时。
现场演示
jQuery 1.8+承诺
Promise.resolve(
$.ajax({
url: '/getData',
timeout:3000 //3 second timeout
})
).then(function(){
//do something
}).catch(function(e) {
if(e.statusText == 'timeout')
{
alert('Native Promise: Failed from timeout');
//do something. Try again perhaps?
}
});
jQuery 1.8 +
$.ajax({
url: '/getData',
timeout:3000 //3 second timeout
}).done(function(){
//do something
}).fail(function(jqXHR, textStatus){
if(textStatus === 'timeout')
{
alert('Failed from timeout');
//do something. Try again perhaps?
}
});
jQuery <= 1.7.2
$.ajax({
url: '/getData',
error: function(jqXHR, textStatus){
if(textStatus === 'timeout')
{
alert('Failed from timeout');
//do something. Try again perhaps?
}
},
success: function(){
//do something
},
timeout:3000 //3 second timeout
});
注意,textStatus参数(或jqXHR.statusText)将让您知道错误是什么。如果您想知道失败是由超时引起的,这可能很有用。
error(jqXHR, textStatus, errorThrown) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests.
src: http://api.jquery.com/jQuery.ajax/
你可以像这样在ajax选项中使用超时设置:
$.ajax({
url: "test.html",
timeout: 3000,
error: function(){
//do something
},
success: function(){
//do something
}
});
阅读关于ajax的所有选项:http://api.jquery.com/jQuery.ajax/
请记住,当超时发生时,触发的是错误处理程序而不是成功处理程序:)
请阅读$。Ajax文档,这是一个涉及的主题。
$.ajax({
url: "test.html",
error: function(){
// will fire when timeout is reached
},
success: function(){
//do something
},
timeout: 3000 // sets timeout to 3 seconds
});
您可以通过访问error: function(jqXHR, textStatus, errorThrown)选项的textStatus参数来查看抛出的错误类型。选项为timeout、error、abort和parsererror。
使用全功能的。ajax jQuery函数。 以https://stackoverflow.com/a/3543713/1689451为例进行比较。
无需测试,只需将您的代码与引用的SO问题合并:
target = $(this).attr('data-target');
$.ajax({
url: $(this).attr('href'),
type: "GET",
timeout: 2000,
success: function(response) { $(target).modal({
show: true
}); },
error: function(x, t, m) {
if(t==="timeout") {
alert("got timeout");
} else {
alert(t);
}
}
});
如果你的请求通过NginX,不要忘记检查NginX设置。
Ajax选项。超时是一回事,但nginx请求超时可能也需要调整。
参见https://ubiq.co/tech-blog/increase-request-timeout-nginx/
您的请求应该如下所示。
client.ajax({
url:'web-url',
method: 'GET',
headers: 'header',
timeout: 3000
});