user823527
user823527

Reputation: 3712

Ajax call within jquery each loop

Here is a function that I have to write to an xml file through an ajax call. The code works fine the first time the ajax call is made. On the second each loop, the ajax call isn't made at all. I don't know why. I specified asyn to false. That did not help. That doesn't seem to be the problem anyway.

$('#'+divid).children('div').children('div').each(function () {

    var url = $(this).find('a');
    var urlname = url.text();
    var urllink = url.attr('href');
    var urlid = $(this).attr('id');

    alert ("from javascript urlid: "+urlid+" urlname: "+urlname+" urllink: "+urllink);

          $.ajax({
             url: "add_url.php",
             type: "POST",
             data: { nodeid: divid, urlid: urlid, urlname: urlname, urllink: urllink },
             cache: false,
             async: false, 
             success: function (response) {
             if (response != '') 
                {
                    alert(response);
                 }
             }
         });
});

Upvotes: 2

Views: 3141

Answers (2)

genesis
genesis

Reputation: 50966

This really works for me

http://jsfiddle.net/genesis/DTjZQ/4 (3 POST request sent with response status 404)

be sure that your html is good and with same structure as in my fiddle

Upvotes: 1

Corbin
Corbin

Reputation: 33437

Instead of making multiple AJAX requests, I suggest appending the data to an array and then sending the entire array of objects.

(Basically the literal object you're using for data would be appended to an array instead of used in a request, and then once the each is done, you would send the array as the data.)

Upvotes: 0

Related Questions