我向服务器发出ajax请求.有时我收到502错误.所以,如果发生这种情况,则会调用error()方法.
如果收到错误,我该如何重复请求?代码应如下所示:
$.ajax({
url: 'http://server/test.php',
type: 'GET',
dataType: 'jsonp',
cache: 'false',
timeout: 32000,
success: function(data) {
//some actions here
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error[refresh]: " + textStatus);
console.log(jqXHR);
// here I want to repeat the request like "this.repeat()"
},
});
Run Code Online (Sandbox Code Playgroud)
你可以这样做,
function ajaxCall(){
$.ajax({
url: 'http://server/test.php',
type: 'GET',
dataType: 'jsonp',
cache: 'false',
timeout: 32000,
success: function(data) {
//some actions here
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error[refresh]: " + textStatus);
console.log(jqXHR);
ajaxCall(); // recursion call to method.
},
});
}
Run Code Online (Sandbox Code Playgroud)
将代码置于函数中并再次调用此函数.喜欢:
function ajaxFunction()
{
....
error:function(){ajaxFunction();}
}
Run Code Online (Sandbox Code Playgroud)