user3218094
user3218094

Reputation: 39

Ajax request response order

I have small doubt about the ajax request

  1. If I send a couple of AJAX request, will response received in that same order ? req1, req2 and then response for req1 is received and then response for req2. Is there any possibility that response for req2 is first received ??

  2. I am not storing column_index value and when I receive response, Will it pickup correct column_index?

Code:

function make_ajax_call(column_index) {
    jQuery.ajax({
        url: site.adminajaxurl + '?action=display_items',
        type: 'POST',
        data: {
            id: dbase_header[column_index]
        },
        // dataType: "json",
        success: function(data) {

            // Until we get all responses hide the submit button
            jQuery('body')
                .find('#submit_template_div')
                .hide();

            // Store the html content received for a template
            data_html[column_index] = data;

            // Increase response count
            response_display_item++;

            // If all response received
            if (response_display_item == request_display_item) {
                // Reset request count and update framework
                response_display_item=0;
                request_display_item = 0;

                print_menu = 1;
                update_framework();
            }
        }
    });
}

Upvotes: 1

Views: 2004

Answers (3)

Tech Savant
Tech Savant

Reputation: 3766

The order is not guaranteed. What you can do if you want to use asynchronous is send a unique key with your request, and then also include that unique key in the response.

Here is a function to create random string...

function randomString(length, chars) {
    var result = '';
    for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
    return result;
}
var rString = randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');

Upvotes: -1

suish
suish

Reputation: 3343

Short answer first.

Q1 the order is not guaranteed(usually)

Q2 Yes

1) Ajax method will be executed asynchronously unless You set async: false.So callbacks execution orders are all depends on the server side. But once you use async: false option,Javascript can not do anything until you receive a responce. which is something users feel like a browser is freezing. So if you don't want "freeze" and keep the order you want at the same time, call next ajax in the first ajax calls callback.

2) Since the Ajax method is called inside of make_ajax_call, ajax callback also have column_index's reference. everytime You call make_ajax_call,javascript creates new name space which means even if you call make_ajax_call while another make_ajax_call method is running, both of their callbacks has a reference to its column_index.

Upvotes: 0

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

1) For asynchronous request, it depends on the time it takes for a specific operation to complete. Only once the operation is complete the response would be received by the client.

If you need sequential order then you should be using synchronous calls to server. If you are using jquery.Ajax method, then you can specify the Async = false.

From JQuery Docs

The first letter in Ajax stands for "asynchronous," meaning that the operation occurs in parallel and the order of completion is not guaranteed. The async option to $.ajax() defaults to true, indicating that code execution can continue after the request is made. Setting this option to false (and thus making the call no longer asynchronous) is strongly discouraged, as it can cause the browser to become unresponsive.

Upvotes: 3

Related Questions