How to cancel/abort/stop jQuery AJAX request?

Hello,

How to cancel/abort/stop jQuery AJAX request?

Can you help?

function stopmyajax(){
       abort();
      }
      
      
    var str = 'grup=1';
    var t = $('#f').serialize();
    (t !='')? str += '&'+t :'';
    $.ajax({
       type: "POST",
       url: "myfile.php",
       data: str,
       success: function(data){       
       alert(data);
       }
      });

Thank you in advance

You can try setting a timeout for it.

Is not timeout. I want to cancel Manual to file myfile.php

I don’t believe there is a way. Once it’s clicked about the thing u can do is close the tab.

Not close the tab,
Running want to stop this file myfile.php
I want to stop with ajax, ajax code in this file ajaxfile.php

I’m sorry my English is bad, I have used google translation

I get what you’re asking but once the script is running, I d OK my know of a way to stop it other than closing the tab it’s running in.

The jquery ajax method returns a XMLHttpRequest object. You can use this object to cancel the request.

The XMLHttpRequest has a abort method, which cancels the request.
Note: If the request has already been sent to the server then the server will process the request even if we abort the request but the client will not wait for/handle the response.

The xhr object also contains a readystate which contains the state of the request(UNSENT-0, OPENED-1, HEADERS_RECEIVED-2, LOADING-3 and DONE-4). we can use this to check whether the previous request was completed.

$(document).ready(
var xhr;

var fn = function(){
    if(xhr && xhr.readystate != 4){
        xhr.abort();
    }
    xhr = $.ajax({
        url: 'ajax/progress.ftl',
        success: function(data) {
            //do something
        }
    });
};

var interval = setInterval(fn, 500);

);

For Web Apps visit http://www.ati-erp.com
ATI-ERP
Expeditous Business Solutions…

Sponsor our Newsletter | Privacy Policy | Terms of Service