0

I'm trying to use ajax in a loop of tr.

Heres my code :

var i=0;
$("tr").each(function() {
var row = datastring;
console.log("I = " + i);
$.ajax({
    type: 'POST',
    url: 'somelinkgoeshere.com',
    data: row,
    success: function(){console.log("Success")},
    error: function(){console.log("Error")}
});
i++;
});​

Expected Result

So I want events to be fired in order where console.log must return logs in following order:

  1. I = 0
  2. Success or Error
  3. I = 1
  4. Success or Error

Actual Result

But after I run the code the console.log returns logs in following order

  1. I = 0
  2. I = 1
  3. Success or Error
  4. Success or Error

So it means my ajax function is called after my each loop is complete. But I dont want the function to loop unless ajax request is compete.

Let me know if you need more explanation.

Thanks.

2
  • 1
    Ajax is asynchronous, running it in async:false mode will result in a bad UX (the browser will lock until your loop is finished, it will look like your page is broken) Commented Oct 31, 2012 at 14:22
  • Ajax is asynch... hey, beat me to it! +1 Commented Oct 31, 2012 at 14:26

3 Answers 3

2

As mentioned mutiple times, Ajax is asynchronous. This means that other functions and code can run at the same time, the benefit of this is that the browser doesn't have to wait for a request to finish before continuing with the page JS execution. Imagine what it would be like if you have a dozen Ajax requests on a page and one server takes 30 seconds to respond...

However, if you still want to run the requests synchronously, you can set the async flag to false:

var i = 0;
$("tr").each(function() {
    var row = datastring;
    console.log("I = " + i);
    $.ajax({
        type: 'POST',
        url: 'somelinkgoeshere.com',
        data: row,
        async: false,
        success: function() {
            console.log("Success")
        },
        error: function() {
            console.log("Error")
        }
    });
    i++;
});​

P.S. You don't need to manually increment i, jQuery's .each() can provide it:

$("tr").each(function(i) {
    var row = datastring;
    console.log("I = " + i);
    $.ajax({
        type: 'POST',
        url: 'somelinkgoeshere.com',
        data: row,
        async: false,
        success: function() {
            console.log("Success")
        },
        error: function() {
            console.log("Error")
        }
    });
});​
Sign up to request clarification or add additional context in comments.

Comments

1

You'd have to set async to false.

$.ajax({
    type: 'POST',
    url: 'somelinkgoeshere.com',
    async: false,
    data: row,
    success: function(){console.log("Success")},
    error: function(){console.log("Error")}
});

This is not the best thing to do though, as it may lock up the browser, and the whole point of AJAX is to be asynchronous.

Comments

1

Use async=false.

Here is a quote from the jQuery documentation:

asyncBoolean Default: true By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the complete/success/err

or callbacks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.