方法POST,状态(已取消)错误消息
Method POST, Status (canceled) error message
我有以下代码,它给了我一条Method POST, Status (canceled)
错误消息:
$(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.");
}
});
实际上,只要每个ajax请求完成,代码就可以工作,但如果我在txt1
中快速键入,即在上一个请求完成之前键入下一个字符,我就会得到错误消息Method POST, Status (canceled)
。
有人知道为什么会发生这种情况以及如何纠正错误吗?
我想这个问题很容易。如果调用xhr.abort();
,则会为挂起的请求调用$.ajax
的error
回调。所以您应该在error
回调中忽略这种情况。因此error
处理程序可以修改为
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
}
附言:也许我对封闭问题的另一个老答案也会让你感兴趣。
这是因为您正在调用abort
方法,该方法可能会用适当的错误消息触发错误处理程序。
您可以等待上一次ajax请求完成,然后再进行下一次调用。
为了解决问题并节省Ajax调用量,我编写了以下示例。此示例允许您处理以下两种情况:
情况1:
The user types slow enough (lets say about one key every 200+ miliseconds
情况2:
The user types fast (my average is about 20 to 50 miliseconds per key)
在下面的示例中,不需要中止或忽略Ajax调用,您不是在滥发Ajax调用,而是在使用对象来处理您的工作。(我甚至帮你弄过了)
var Handler = {
/**
* Time in ms from the last event
*/
lastEvent: 0,
/**
* The last keystroke must be at least this amount of ms ago
* to allow our ajax call to run
*/
cooldownPeriod: 200,
/**
* This is our timer
*/
timer: null,
/**
* This should run when the keyup event is triggered
*/
up: function( event )
{
var d = new Date(),
now = d.getTime();
if( ( now - Handler.lastEvent ) < Handler.cooldownPeriod ) {
// We do not want to run the Ajax call
// We (re)set our timer
Handler.setTimer();
} else {
// We do not care about our timer and just do the Ajax call
Handler.resetTimer();
Handler.ajaxCall();
}
Handler.lastEvent = now;
},
/**
* Function for setting our timer
*/
setTimer: function()
{
this.resetTimer();
this.timer = setTimeout( function(){ Handler.ajaxCall() }, this.cooldownPeriod );
},
/**
* Function for resetting our timer
*/
resetTimer: function()
{
clearTimeout( this.timer );
},
/**
* The ajax call
*/
ajaxCall: function()
{
// do ajax call
}
};
jQuery( function(){
var field = jQuery( '#field' );
field.on( 'keyup', Handler.up );
});
希望这能有所帮助。
您正在使用keyup
事件,这似乎是问题所在。
如果有什么不同的话,你需要在输入一个字符后等待才能采取行动。
一个更好的解决方案可能是遵循与JQuery AutoComplete COmponent
相同的策略。
Ajax是一种异步类型,它不建议u在每个keyup事件上发送请求,请尝试。。。
async: false
在post方法中。。。它将暂停后续的帖子,直到当前请求完成其回调
实际上,您需要一个setTimeout方法来防止触发冗余的ajax调用。
clearTimeout(timer);
if($("#txt1").val().length >= 2){
timer = setTimeout(function(){
get_data($("#txt1").val());
}, 400);
}else{
get_default();
}
这应该会消除你的问题。
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- jquery点击函数select&取消选择
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- javascript onchange取消绑定错误|未定义
- fs extra:错误:EPERM:不允许操作,取消链接
- Bootstrap 3错误地取消折叠类
- 节点 JS 取消处理程序错误事件
- 方法POST,状态(已取消)错误消息
- 使用jquery使用文本框取消隐藏选项卡会取消隐藏错误的部分
- 如何取消委托点击错误
- 区分ajax错误中的请求失败(无网络)和请求取消(HTTP状态码0)
- 为什么我得到错误:“试图取消渲染未渲染的片段”;从reactive .js 0.3.9迁移到0.5.5时
- 为什么我的https://请求取消并更改为http://,导致混合请求错误
- 在Boomi计时器已经取消的uCanAccess错误
- 位操作 - 在 JavaScript 中取消设置产生错误值的位
- 捕捉.svg悬停/取消悬停错误(用于更长的悬停)
- Dojo MVC:如何在数据错误时取消视图转换
- 如何取消上传而不显示错误