I have the following code which is giving me a Method POST, Status (canceled)
error message:
$ (document). Ready (function() { varXHR =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 (); } }); functionGet_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) {varErr = eval ("(" + Xhr.responsetext + ")"); Alert (Err. Message); } }); } functionGet_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 characte R before the previous request finishes, I get the error message Method POST, Status (canceled)
.
Anyone know why are happening and how to correct the error?
Answer1:
I Suppose that the problem was very easy. If you call then the callback of'll is called for the xhr.abort();
error
$.ajax
pending Request. So, should just ignore such case inside of error
callback. So the error
handler can is 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 IGN ORed and no error message be displayed }
P.s. Probably another my old answer in the close problem could also interesting for you.
Answer2:
That's because 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 a async type, its isn't recommonded that u to send request on every KeyUp event, try the The ...
async: false
In Post method ... it ' ll pause the subsequent posts until the current request do its callback
"Reprint" Http://stackoverflow.com/questions/9928580/method-post-status-canceled-error-message
Method POST, Status (canceled) error message