943,789 Members | Top Members by Rank

Ad:
Dec 20th, 2006
0

Help about AJAX.

Expand Post »
I am developing a page with many of AJAX methods.

what i want to do is, inside a javascript function, there is a loop. inside this loop i want to call the AJAX methods and retrieve the response text.

when i do that i recieve an exception telling me that the data is not available yet. because the readyState is 1 (loading) not 4 (finish).

how can i access the response text while the readyState is 1?

thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khusani is offline Offline
13 posts
since Oct 2006
Dec 20th, 2006
0

Re: Help about AJAX.

you shouldn't access the response text until it is 'there',

what you should do is either wait for the request to reach the ready state 4 (best achieved by using an event listener), or set the request to syncronous, by setting the last argument in an 'open' method to false:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. xmlHttp.open("GET", url, true);
  2. <any code here will be executed as soon as the open method has been called
  3. xmlHttp.open("GET", url, false);
  4. <any code here will be executed when the request is finished.

is there any reason you want to access the responseText before it has finished?
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Dec 31st, 2006
0

Re: Help about AJAX.

Your problem is, that the subsequent calls to the ajaxrequest are using the same global variable for the request. The posted answer is right in the point to make the calls synchronous. Other thing you can do is to use namespacing and nested functions to create new httprequest object for each ajax call.

Other thing is, you can read data partially send, but that is ready state 3 and is considered unsafe (but can be done, you just have to make sure you know what you've read and what not when the readystate changes to 4 and the rest of the data arrives). Readystate 1 means you just sent the request, but no response is back yet.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nemo5 is offline Offline
6 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: AJAX : Am i right with the concept?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: how to access innerhtml element





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC