2

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);
                 }
             }
         });
});
5
  • 1
    Setting async: false in the ajax call won't have an effect, because you are looping in an own function, which is getting called asynchronously by jQuery to iterate over all elements. Each executed function then will wait for itself for the ajax call to finish. But they will all run at the same time. Commented Sep 5, 2011 at 19:14
  • 1
    @Wulf: The callback functions are not called asynchronously: github.com/jquery/jquery/blob/1.6.3/src/core.js#L609-L647 Commented Sep 5, 2011 at 19:38
  • I have used the same format, ajax in an each() function and it works fine. Have you checked in Firebug that only one POST request is being made and is there definitely more than 1 object. Do you get multiple alerts? Commented Sep 5, 2011 at 20:14
  • @MattP There is definitely more than one object. The alert statements show all the objects. But the ajax call is only made once. The second time that php function isn't called. Commented Sep 6, 2011 at 0:37
  • Can you use Firebug to check if only 1 POST request is being made, it should work, someone else has tried it and it works so you need to see if multiple POST requests are being made but there is some kind of PHP error which means they don't complete the expected task. Firebug Console should help a lot with this kind of debug. Commented Sep 6, 2011 at 10:56

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I was also able to get the alert statements showing that the each function iterates through all the children. The problem was that the php function was not being called. When I tested the fiddle, I didn't get the response confirming that the php was called.
Were you able to put a response in the php function to confirm that it is called three times?
@user823527: because fiddle do not support PHP. In your case, there's problem in PHP not in JS
The problem was in the PHP function. Once that worked, the .each looped through all the children.
0

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.)

1 Comment

I thought of that. I was just wondering why the ajax call was only made once. I will see how the array works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.