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.");
} });

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?

Answer1:

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. Probably another my old answer on the close problem could also interesting for you.

Answer2:

That is because you are calling abort method which possibly triggers the error handler with appropriate error message.

You can possibly wait for previous ajax request to complete before making the next call.

Answer3:

Ajax is an async type, its not recommonded that u to send request on every keyup event, try the...

async: false

in post method... it'll pause the subsequent posts until the current request done its callback

【转载】http://stackoverflow.com/questions/9928580/method-post-status-canceled-error-message

最新文章

  1. ajaxFileUpload 异步上传数据
  2. 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案
  3. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德
  4. 《C与指针》第十章练习
  5. EF6+MYSQL之初体验
  6. hdu 2060
  7. 理解CSS3里的Flex布局用法
  8. URAL 2068 Game of Nuts (博弈)
  9. 大学生IT博客大赛最技术50强与最生活10强文章
  10. 【HDOJ】2853 Assignment
  11. Android版本铎A梦幻连连看游戏源代码完整版
  12. php 模拟POST提交的2种方法
  13. RabbitMQ和SpringBoot的简单整合列子
  14. MySQL查询性能优化一则
  15. .NET-记一次架构优化实战与方案-目录
  16. Project with Match in aggregate not working in mongodb
  17. JDK源码分析之hashmap就这么简单理解
  18. Hbase记录-Hbase基础概念
  19. http put post请求区别
  20. hadoop的五个守护进程【转】

热门文章

  1. MySQL优化查询相关
  2. java将HSSFWorkbook生成的excel压缩到zip中
  3. Java学习十七
  4. Python—异步任务队列Celery简单使用
  5. cuda addressMode解析
  6. 收藏基本Java项目开发的书
  7. Thinkcmf子栏目获取父级栏目所有子栏目列表
  8. 素小暖讲JVM:Eclipse运行速度调优
  9. springboot配置多个yml文件
  10. npm安装依赖报 npm ERR! code Z_BUF_ERROR npm ERR! errno -5 npm ERR! zlib: unexpected end of file 这个错误解决方案