方法POST,状态(已取消)错误信息 [英] Method POST, Status (canceled) error message
问题描述
我有以下的code这是给我一个方法POST,状态(已取消)
错误信息:
I have the following code which is giving me a Method POST, Status (canceled)
error message:
$(document).ready(function() {
var xhr = false;
get_default();
$('#txt1').keyup( function() {
if(xhr && xhr.readyState != 4){
alert("abort");
xhr.abort();
}
if ($("#txt1").val().length >= 2) {
get_data( $("#txt1").val() );
} else {
get_default();
}
});
function get_data( phrase ) {
xhr = $.ajax({
type: 'POST',
url: 'http://intranet/webservices.asmx/GetData',
data: '{phrase: "' + phrase + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function( results ) {
$("#div1").empty();
if( results.d[0] ) {
$.each( results.d, function( index, result ) {
$("#div1").append( result.Col1 + ' ' + result.Col2 + '<br />' );
});
} else {
alert( "no data available message goes here" );
}
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message) ;
}
});
}
function get_default() {
$('#div1').empty().append("default content goes here.");
}
});
在code的实际工作,只要每一个Ajax请求完成,但如果我输入快进 TXT1
,也就是之前的previous键入下一个字符请求结束,我得到错误信息方法POST,状态(已取消)
。
The code actually works as long as each ajax request completes, but if I type fast into txt1
, i.e. type the next character before the previous request finishes, I get the error message Method POST, Status (canceled)
.
谁知道为什么发生这种情况,以及如何纠正该错误?
Anyone know why this is happening and how to correct the error?
推荐答案
我想,这个问题是很容易的。如果你调用 xhr.abort();
则错误
$回调AJAX
将为挂起的请求<叫 / STRONG>。所以,你应该忽略的错误
回调里面这种情况。因此,错误
处理程序可以修改,以
I suppose that the problem is very easy. If you call xhr.abort();
then the error
callback of $.ajax
will be called for the pending request. So you should just ignore such case inside of error
callback. So the error
handler can be modified to
error: function(jqXHR, textStatus, errorThrown) {
var err;
if (textStatus !== "abort" && errorThrown !== "abort") {
try {
err = $.parseJSON(jqXHR.responseText);
alert(err.Message);
} catch(e) {
alert("ERROR:\n" + jqXHR.responseText);
}
}
// aborted requests should be just ignored and no error message be displayed
}
P.S。大概在接近问题的另一个我老答案也感兴趣的你。
这篇关于方法POST,状态(已取消)错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!