How to handle multiple ajax requests one after another
I am in a need to have a jQuery solution to issue ajax requests one after another.
User input are fetch in to an array.Then after iterating through the array, I have planned to pass each array element (a string) to a specific url through ajax post, then on success , it needs to update a particular html element.
But each ajax request should be made after, processing of previous ajax request, receving its content, and html element update.
How can implement it..
var arr = new Array("string1","string2","string3","string4");
ids of HTML elements : e1 , e2, e3, e4
Ajax URL : processAjax.php
data variable : str (each above array element to be pass as str with Ajax URL)
Can someone please explain me how to do it with above parameters.
Be default, jQuery AJAX requests are performed asynchronously. It sounds like you want need to wait for one request to finish before starting the next, so you'll want to set the async to false on your AJAX command.
.... the rest of your lines
I'm always up for networking with fellow web professionals. Connect with me on LinkedIn
if you like!
You could use async:false as cbVision suggested, but that will freeze (block) the browser until the requests are complete.
I'm not going to build your code for you, but here's a working example I made for Greasemonkey a few months ago.
It uses Greasemonkey's GM_xmlhttpRequest but I'm sure you can switch that to the regular xmlhttpRequest, or even $.ajax.
My method uses the onload of an xmlhttpRequest to start the next request. It's asynchronous and doesn't freeze the browser, but the URLs will get requested in order, successively.
You can pass runQueue() (in any order) a delay to use between requests, a callback function, and an array of URLs to send requests to.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)