1

I want to call webservice with multiple data's at a time.So here i have used ajax with in the for loop for sending the reguest one by one.But in my below code, only the first value of the array is passed due to the asynchronous ajax call.

How can I pass each iteration of the array to the callback function, aside from running the ajax call synchronously?

The following is my loop structure.I hope any body will help me,thanks in advance.

var jsonarray=new Array();
for (var i = 0; i < results.rows.length; i++) 
{                           
    jsonarray.push(results.rows.item(i));                   

    $.ajax
    ({  
        url:www.webservice.com,
        type:'POST',
        dataType:'jsonp',
        contentType: "application/json; charset=utf-8",
        timeout:1000000,
        data: {data: JSON.stringify(jsonarray)}, 
        success:function(data)
        {               
        ...      
        },
        error:function()
        {
        }
    });
}
cheliyan
  • 1,679
  • 3
  • 16
  • 22
  • 2
    Well it depends on the webservice. Some webservices allow "bulk requests" where you can provide for example a list of ID's and the webservice returns a rowset for each ID provided. Maybe your webservice provides such an interface and no "loop" is necessary on your side. If there is no "bulk"-method you can take a look at ["sequencing ajax requests"](http://stackoverflow.com/questions/3034874/sequencing-ajax-requests), which provides some pointers on how to do it cleanly. – vstm Aug 17 '13 at 07:38

0 Answers0