Method POST, Status (canceled) error message

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.