Jump to content

getting more than one response???


acidglitter

Recommended Posts

Can't understand what you want to do..

But as for my understanding, do you want 2 replies in one request?

 

Correct me if I'm wrong...

In AJAX, 1 request = 1 reply only.

 

Here are some options that I can recommend:

  1. Call another request after the first one.

  2. Have a delimiter in your responseText to separate the reply.

 

Link to comment
Share on other sites

If what you want to do is update multiple DIVS for example, you can use a unique string to separate div name from value.  IE here is an onFinish function I used to process multiple divs from one responseText.  I am sure pro coders will cringe at this but it worked for me.  I am sure you could also use json or XML responses to do it as well...

 

 

      function onFinish(req)

      {

            var result = req.responseText;

            var resultArray = result.split("_____"); //splits off the first part(can be repeated for multiple divs) of the array which contains the div that is to be updated, _____ is just a unique character combination

            var max_i = resultArray.length;

            //this loop cycles through the array and to grab each div ID and update the content associated with it

            for (var i=0; i<=max_i; i = i+2)  {

              var h = i+1 ; //used to set array position for data to fill selected div

              var div = resultArray;  //the div to be updated

              var displayResult = resultArray[h]; // takes the second part of the response that will be displayed in the desired div

              $(div).innerHTML= displayResult;  //display result in the selected div

             

            }

      }

 

What this does is takes a text response say for example,  "div1_____junk to put in div1_____div2_____junk to put in div2" and will split it into an array in javascript by separating at the "_____".  Then it loops through the array taking two array locations at a time, var h and var i.  these will be the div name and div value respectively.  It then uses these values to update the divs and incremnts up by two to get the next two values in the array if there are more. 

 

Hope this helps.  Like I said I am a beginner myself, so I am sure there are many better ways of accomplishing this, but for now, it works.

 

Good luck.

Link to comment
Share on other sites

Sorry to hijack...

 

What about if you did want two different responses, namely, one from one PHP script and one from another. How would one go about making two separate requests from the same function without one response overwriting the other in the HTTPObj?

 

I'm guessing that you would need to create a new request object, I can't figure out how to create a separate instance of the object.

 

Cheers

Dan

Link to comment
Share on other sites

You are correct, you would need another XHR object.

 

This might help:

http://javascript.about.com/library/blajax13.htm

 

One thing to keep in mind when using XHR is that a browser can only have two simultaneous requests active at a time.  Hence more complicated XHR programs don't invoke the request immediately when the client code makes the call.  Instead, the parameters of the request are stored in a priority queue with automatic promotion and another piece of code dequeues and invokes the requests as resources are available.

 

For example, say the client code wants to make 5 simultaneous XHR requests A, B, C, D, and E with priorities 1, 2, 4, 2, and 1 respectively.  Assuming lower numbers represent high priority, these requests go into a queue:

(A,1), (E,1), (B,2), (D,2), and (C,4)

 

The browser can only ever have two requests open at a time.  So the XHR server-code keeps track of the number of requests currently active:

var n_xhr_requests = 0;

 

We have zero requests so we can dequeue a request and invoke it; this removes (A,1) and increments n_xhr_requests to 1.

 

We have one request so we can dequeue another and invoke it; this removes (E,1) and increments n_xhr_request to 2.

 

We are using all our requests so we stop dequeuing.  Meanwhile, request (F,3) is enqueued, changing our queue to:

(B,2), (D,2), (F,3), and (C,4)

 

Request (E,1) returns so we can dequeue the next one (B,2) and invoke it.

 

Let's assume requests with a priority of 1 through 3 inclusive keep coming in.  They will always be ahead of (C,4) so our (C,4) request will never happen.  This is where the automatic promotion behavior of a priority queue comes in handy.  If any item sits in the queue for too long, it's priority is automatically increased.  Hence after some duration of time, (C,4) will change to (C,3).  If (C,3) happens to sit in there long enough again, it will be promoted to (C,2).  Eventually via automatic promotion C will be invoked.

 

Fun stuff, eh?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.