Making AJAX Synchronous?

B

bobmct

Stoopid question, I know. But, I'm running into the proverbial
problem where I'm executing two Ajax requests from an onload request.
Either the second fails or the first is polluted by the data returned
by the second (long story).

Normally they work just find when executed as a result of the onchange
request. It's only on a page refresh that the onload is being used.

Although I will continue researching and trying various techniques to
keep them unique, I am hoping that those more skilled than I can
respond with "Oh, just do this..."

Hoping and wishing is not all wrong.

Thanks anyone.
 
T

Thomas 'PointedEars' Lahn

bobmct said:
Stoopid question, I know. But, I'm running into the proverbial
problem where I'm executing two Ajax requests

What do you call an "Ajax request"?
from an onload request.

A *what*? `onload' is an event handler for the `load' event; it has as much
to do with requests like "key" with "keyboard".
Either the second fails or the first is polluted by the data returned
by the second (long story).

Apparently you are using a bad library.
Normally they work just find when executed as a result of the onchange
request.

A *what*? `onchange' is an event handler, too, for the `change' event.
It's only on a page refresh that the onload is being used.

Obviously now, you are either using a bad library, or are lacking the
minimum clue to use the library, or both. The `onload' code (_not_:
request) should be executed every time the document is loaded.
Although I will continue researching and trying various techniques to
keep them unique, I am hoping that those more skilled than I can
respond with "Oh, just do this..."

Use or (better) write a proper XHR library, or use the existing library
properly.

Suppose the two "Ajax requests" specify two event listeners for the
`readystatechange' event of an XHR object, that modify the same HTML
fragment, and that is the problem, you would need to issue the second
request (with the send() method of the XHR object) only after the first
response was fully received (as indicated by the `readyState' property
of the object having the value 4).
Hoping and wishing is not all wrong.

(Interesting typo.)

You have yet to describe the real problem, so there is no telling what you
could be doing wrong (besides maybe using a bad library, or using a library
improperly).

<http://jibbering.com/faq/#posting>


PointedEars
 
L

Lasse Reichstein Nielsen

bobmct said:
Stoopid question, I know. But, I'm running into the proverbial
problem where I'm executing two Ajax requests from an onload request.
Either the second fails or the first is polluted by the data returned
by the second (long story).

Are you reusing the XMLHttpRequest object?
If you are doing two requests at the same time, they need to be done
on separate XMLHttpRequest obejcts.
Normally they work just find when executed as a result of the onchange
request. It's only on a page refresh that the onload is being used.

Are you doing two requests in the onchange event handler as well, or
only one?
Although I will continue researching and trying various techniques to
keep them unique, I am hoping that those more skilled than I can
respond with "Oh, just do this..."

Without seing the page, it's hard to be sure, but pollution between
requests suggests that you are using the same request object twice.
So, oh, just don't do that. :)

If all you want is to make the request synchroneous, as the subject
suggests, change the third parameter of the call to 'open' to false.
That will make the 'send' call blocking, so that it won't return until
the requests has completed (successfully or not).

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Without seing the page, it's hard to be sure, but pollution between
requests suggests that you are using the same request object twice.
So, oh, just don't do that. :)

It is possible and very useful to reuse the same XHR object for
non-concurrent requests.
If all you want is to make the request synchroneous, as the subject
suggests, change the third parameter of the call to 'open' to false.
That will make the 'send' call blocking, so that it won't return until
the requests has completed (successfully or not).

The suggestion is theoretically correct, but it should be (and has been)
practically recommended against. For setting the third argument to `false'
will block the current global execution environment. And because it keeps
the TCP connection open it is very likely to block other requests in other
global execution environments (e.g. other windows or tabs) as the number
concurrent TCP connections a browser's HTTP client is going to establish, is
limited by vendor's or user's preferences (usually 2 to 4 concurrent
connections). This would go against user's expectations that "Ajax" would
make the Web application more responsive than
"Web 1.0".


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top