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

Method POST, Status (canceled) error message的更多相关文章

  1. 记一次接口调试错误: {"timestamp":"2019-09-11T03:04:30.036+0000","status":500,"error":"Internal Server Error","message":"Could not write JSON: Object is null; nested exception is com.fasterxml.jackson

    接口测试中用postman测试返回是正常的,但是使用其他人去调用就出错了,找了半天,才想起来使用了nginx,用于端口的代理转发.然后根据错误信息发现json格式的某个字段为null,结合日志中的报文 ...

  2. {"timestamp":"2019-11-12T02:39:28.949+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'text/plain;charset=UTF-8' not supported","path":&quo

    在Jmeter运行http请求时报错: {"timestamp":"2019-11-12T02:39:28.949+0000","status&quo ...

  3. Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"

    Article ID: 939308 - View products that this article applies to. Expand all | Collapse all Symptoms ...

  4. undefined reference to typeinfo - C++ error message

    undefined reference to typeinfo - C++ error message There are some compiler and loader error message ...

  5. Delphi HTTP error message: Can't execute C:\Program Files\Borland\Delphi7\Bin\serverinfo.exe 1813

    delphi  调用Webservice ,停止服务的时候总是爱提示: Internal Server ErrorHTTP status code: 500 HTTP error message: C ...

  6. tomcat已启动,使用maven的deploy发布后,根据路径打开浏览器访问时报错HTTP Status 500 - Error instantiating servlet class

    web项目中请求出现错误,如下: HTTP Status 500 - Error instantiating servlet class XXXX类 type Exception report mes ...

  7. Nginx not running with no error message

    Nginx not running with no error message #!/bin/shecho "start"rm /etc/nginx/sites-enabled/d ...

  8. HTTP Status 500 - Error instantiating servlet class XXXX

    问题描述 web项目中请求出现错误,如下:  HTTP Status 500 - Error instantiating servlet class XXXX类  type Exception rep ...

  9. ABAP术语-Error Message

    Error Message 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/30/1058283.html Information from ...

随机推荐

  1. mac下使用opencv编译安装新模块contrib

    opencv-4.0.1 opencv_contrib-4.0.1 提供ippicv下载链接: https://pan.baidu.com/s/1OIJRUqPqAtpMetku8qX36w cont ...

  2. 基于libcurl的POST(http)

    #include <stdio.h> #include <curl/curl.h> int main (void) { char *url="http://www.n ...

  3. 基于ssh开发彩票购买系统的设计与实现毕业设计

    开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MYSQL数据库 运行效果图: 源码及原文地址:http://javadao.xyz/forum.php?mod ...

  4. JAVA内存分配-通俗讲解

    Java的内存分配上,主要分4个块: 一块是用来装代码的,就是编译的东西. 一块是用来装静态变量的,例如用static关键字的变量,例如字符串常量. 一块是stack,也就是栈,是用来装变量和引用类型 ...

  5. 01 语言基础+高级:1-6 集合_day04【Map】

    day04 [Map] 主要内容 Map集合 教学目标 能够说出Map集合特点 使用Map集合添加方法保存数据 使用”键找值”的方式遍历Map集合 使用”键值对”的方式遍历Map集合 能够使用Hash ...

  6. PAT Basic 插⼊与归并(25) [two pointers]

    题目 根据维基百科的定义: 插⼊排序是迭代算法,逐⼀获得输⼊数据,逐步产⽣有序的输出序列.每步迭代中,算法从输⼊序列中取出⼀元素,将之插⼊有序序列中正确的位置.如此迭代直到全部元素有序.归并排序进⾏如 ...

  7. Spring注解配置和xml配置优缺点比较

    Spring注解配置和xml配置优缺点比较 编辑 ​ 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...

  8. Anaconda Installation on Mac: conda command not found 环境变量配置

    Mac系统安装完Anaconda 3.7后在terminal输入conda --version,返回command not found 原因可能是没有配置环境变量 在terminal输入vi ~/.b ...

  9. redis 会丢数据吗

    不管是以前的主从模式(哨兵模式),还是现在的集群模式,因为都用了slave of 同步; 而slave of 同步会丢弃本地数据,直接用对方的数据来覆盖本地,所以会丢失数据 1.主备网络不通,后续主节 ...

  10. aop 实现原理

    aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...