abort previous ajax request

abort previous ajax request

JonnyEgginsJonnyEggins Posts: 4Questions: 1Answers: 0
edited April 2018 in Free community support

Hi there,

I used this code for datatable...

$('#table').DataTable({
            "destroy": true,
            "processing": true,
            "serverSide": true,
            "columnDefs": [{
                "targets": 0,
                "orderable": false
            }],
            "ajax": {
                url: baseURL + checkVal,
                type: "post",
                xhr: function() {
                    var xhr = $.ajaxSettings.xhr();
                    xhr.upload.onprogress = function(e) {
                        xhr.abort(); //abort all current request here
                    };
                    return xhr;
                },
                error: function() {
                    $(".employee-grid-error").html("");
                    $("#table").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
                }
            },
            'initComplete': function(settings, json) {
                //code here
            }
        });

when I abort it will abort all current request... I want to stop all previous request when current request start running ... How to do that...

I used your all code which is posted in answer. But i can't get any value of settings.jqXHR or settings.jqXHR.abort() anywhere...

please help me how to abort all previous request..

Thanks in advance :)

Answers

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin
    edited August 2017
    table.settings()[0].jqXHR
    

    is where you will find that property.

    But I urge cation with the settings() method - it provides access to internal properties. Changing them can have undefined effects!

    Allan

  • JonnyEgginsJonnyEggins Posts: 4Questions: 1Answers: 0

    can you please tell me that where i put this code ?

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    Wherever it is that you are trying to cancel the Ajax request.

    Allan

  • JonnyEgginsJonnyEggins Posts: 4Questions: 1Answers: 0
    edited August 2017

    I want to stop previous ajax request......when new request running then all previous request must be abort. But new request must be running

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    table.settings()[0].jqXHR.abort() before you trigger the next Ajax request.

    Allan

  • JonnyEgginsJonnyEggins Posts: 4Questions: 1Answers: 0

    Thanks allan ......It's working :smile:

  • akarinasantiagoakarinasantiago Posts: 5Questions: 0Answers: 0
    edited October 2017

    Hi. I put it in drawCallback and preDrawCallback and it doesn't work. Could you please help me?

  • akarinasantiagoakarinasantiago Posts: 5Questions: 0Answers: 0

    settings.jqXHR.abort() doesn't do anything

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    Happy to help - please link to a test case showing the issue.

    Allan

  • akarinasantiagoakarinasantiago Posts: 5Questions: 0Answers: 0

    Thanks. The result has more than 2 millions of records. When you search a name in the list, it will force to get 2 million for every letter. It is too much for this database

    https://jsfiddle.net/akarina/3y1fx07k/5/

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    I'm not entirely clear how the 2 million records relates to preDrawcallback? The code you linked to shows you are using server-side processing - does your /api/result end point support server-side processing? It should only be getting 100, 250 or 500 records at a time if so.

    Allan

  • akarinasantiagoakarinasantiago Posts: 5Questions: 0Answers: 0
    edited October 2017

    Sorry for the delay in replying. When you search "a" in the search-box, it can retrieve 2 millions of records. If many people are using the application, it will be worst.

    I have to search all records

  • kthorngrenkthorngren Posts: 20,347Questions: 26Answers: 4,776

    Not sure what DB you are using but your server side search should support a limit (page length) and offset (page number) options to return the specific subset of dat based on the page being viewed.

    Kevin

  • akarinasantiagoakarinasantiago Posts: 5Questions: 0Answers: 0
    edited October 2017

    the search has to be on the 2 millions of records, all of them

  • kthorngrenkthorngren Posts: 20,347Questions: 26Answers: 4,776

    Again, I'm not sure which DB you are using but the limit and offset (might be different for your DB) are placed in the query to limit the records returned to the 2 million records that fall within those parameters.

    However it sounds like your concern is having multiple users searching at the same time. You might be interested in the searchDelay to help throttle the searches. This thread may be of interest too:
    https://datatables.net/forums/discussion/comment/116229/#Comment_116229

    If you want to get settings.jqXHR.abort() working then please post a link to your page or a test case for debugging why its not working.

    Kevin

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin
    edited October 2017

    We'd need to know if you are actually using server-side processing. As I asked above:

    does your /api/result end point support server-side processing?

    Allan

    p.s. Yes - as Kevin says, we'd need a link to a test case showing the issue to be able to offer any help. Currently we are just guessing at the issue.

This discussion has been closed.