Radu Ionuţ
Radu Ionuţ

Reputation: 41

Abort ajax request without affecting the next request

I have a number input and "onChange" event, where I'm triggering this ajax request (see the code). This aborting affects my next request. If I'm sending only one request without aborting , the request has a normal speed but if I'm aborting, like 3 requests, the ajax looks like it's sending, in one request, all the aborted requests...

 globalAjaxChange = $.ajax({
    type: 'POST',
    dataType: 'json',
    url: ajaxUrl,
    data: formData,
    processData: false,
    contentType: false,
    beforeSend: function () {
        if (globalAjaxChange != null) {
            globalAjaxChange.abort();
        }
    },
    success: function (data) {
        if (data['ok'] == false) {
            notify(data['msg'], 'error');
        }
    },
    error: function (data) {
        if (data.statusText !== 'abort')
            notify('message', 'error');
    }
});

Here is the network inspection:

Network Inspection

Upvotes: 1

Views: 239

Answers (1)

CoderLee
CoderLee

Reputation: 3479

Try returning try/false in your beforeSend callback. Unless you need to send the ajax request anyway, jQuery will abort the request for you with a return of false in that function. See the jQuery docs here for more info on the callback methods.

You also might be having a logic or async issue on the set of the globalAjaxChange variable. Which would cause the ajax request to always send by never returning null on your check for globalAjaxChange. Try placing breakpoints around where it sends and gets set at, to see what the values are.

Upvotes: 1

Related Questions