www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to handle multiple ajax requests one after another

  1. #1
    Join Date
    Sep 2004
    Location
    Sri Lanka
    Posts
    470

    Question How to handle multiple ajax requests one after another

    Hi,

    I am in a need to have a jQuery solution to issue ajax requests one after another.

    Like this,

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

    Example

    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.

    Thanks

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,123
    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.

    http://api.jquery.com/jQuery.ajax/

    Code:
    $.ajax({
      url: "test.html",
      async: false,
      .... the rest of your lines
    });
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Jun 2008
    Posts
    106
    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.

    code: http://pastebin.com/JRdStiyB

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles