Abort Ajax requests using jQuery

2018-12-30 23:24发布

Using jQuery, how can I cancel/abort an Ajax request that I have not yet received the response from?

18条回答
孤独总比滥情好
2楼-- · 2018-12-30 23:41

It's an asynchronous request, meaning once it's sent it's out there.

In case your server is starting a very expensive operation due to the AJAX request, the best you can do is open your server to listen for cancel requests, and send a separate AJAX request notifying the server to stop whatever it's doing.

Otherwise, simply ignore the AJAX response.

查看更多
无色无味的生活
3楼-- · 2018-12-30 23:41

I had the problem of polling and once the page was closed the poll continued so in my cause a user would miss an update as a mysql value was being set for the next 50 seconds after page closing, even though I killed the ajax request, I figured away around, using $_SESSION to set a var won't update in the poll its self until its ended and a new one has started, so what I did was set a value in my database as 0 = offpage , while I'm polling I query that row and return false; when it's 0 as querying in polling will get you current values obviously...

I hope this helped

查看更多
查无此人
4楼-- · 2018-12-30 23:42

meouw's solution is correct, but if you're are interested in more control then you could try the Ajax Manager plugin for jQuery.

查看更多
不流泪的眼
5楼-- · 2018-12-30 23:42

there is no reliable way to do it, and I would not even try it, once the request is on the go; the only way to react reasonably is to ignore the response.

in most cases, it may happen in situations like: a user clicks too often on a button triggering many consecutive XHR, here you have many options, either block the button till XHR is returned, or dont even trigger new XHR while another is running hinting the user to lean back - or discard any pending XHR response but the recent.

查看更多
深知你不懂我心
6楼-- · 2018-12-30 23:45

It is always best practice to do something like this.

var $request;
if ($request != null){ 
    $request.abort();
    $request = null;
}

$request = $.ajax({
    type : "POST", //TODO: Must be changed to POST
    url : "yourfile.php",
    data : "data"
    }).done(function(msg) {
        alert(msg);
    });

But it is much better if you check an if statement to check whether the ajax request is null or not.

查看更多
爱死公子算了
7楼-- · 2018-12-30 23:47

You can abort any continuous ajax call by using this

<input id="searchbox" name="searchbox" type="text" />

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

<script type="text/javascript">
     var request = null;
        $('#searchbox').keyup(function () {
            var id = $(this).val();
            request = $.ajax({
                type: "POST", //TODO: Must be changed to POST
                url: "index.php",
                data: {'id':id},
                success: function () {

                },
                beforeSend: function () {
                    if (request !== null) {
                        request.abort();
                    }
                }
            });
        });

</script>
查看更多
登录 后发表回答