Click here to Skip to main content
13,297,266 members (71,126 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 8 Apr 2013

How to Abort a AJAX Call Before Calling the Same Again

, 8 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Aborting an fired Ajax call before firing the same again


I want to show auto search result just below the search box. After each character write, I want to do an Ajax call and get the data from the database. In this case, if someone wrote 4 characters, then 4 Ajax calls will be fired and from all these Ajax calls, the last response will be taken as a search result. Sometimes, it may not be correct because at times, the earlier fired Ajax call can give response after the later fired Ajax call, in this case we may have inconsistent data. To solve this issue, we have to abort the previously fired Ajax calls.

The sample code given below solves the problem.

Using the Code Snippet

<input id="searchbox" name="searchbox" type="text" />
<div id="data"></div>
    var currentRequest = null;
    jQuery('#searchbox').keyup(function() {
        var text = jQuery(this).val();
        currentRequest = jQuery.ajax({
            type: 'POST',
            data: 'search_text=' + text,
            url: 'AJAX_URL',
            beforeSend : function()    {           
                if(currentRequest != null) {
            success: function(data) {


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sisir Patro
Software Developer
India India
Learn the base not only the requirement related things for quick fixing and make others learn the exact thing always...

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
toanloau16-Sep-14 0:25
membertoanloau16-Sep-14 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 8 Apr 2013
Article Copyright 2013 by Sisir Patro
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid