Synchronus XML Request

Often when I'm programming, I find I would be much better served by a synchronus XML HTTP request than an asynchronus one.  For instance, if I'm doing a postback from a popup window I want to close as soon as the data is retrieved, an asynchronus request just won't work properly.

Up until now, I've been using a "magic button" with it's click event registered to an asyncronus version of the function call I want to make, and then calling triggering the click via script before closing the window (this avoids orphaning the XMLHttpObject before any asynchronus callback requests are made).

Now, Ideally, I want to just return the result of a synchronus request DIRECTLY from the calling function... that is, no "onreadystatechange" (callback) function.
LVL 12
netsmithcentralAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IsisagateCommented:
put the window close in the function that is called on the return of your post back... That fakes a sync call... if you do a sync call you might as well just have a standard form and post it to the back cause it's gonna lock up all other processes...   You should be able to change the boolean value on the open to make it sync. set it to false.


0
dmagliolaCommented:
Use "false" as the third parameter in the Open method of XMLHttpRequest.
Instead of:

xml1.open("GET", strurl , true);

use:

xml1.open("GET", strurl , false);

That will make the request synchronous and then right after callign open(), you can get the response in the responseText or responseXML properties as usual.

Synchronous requests are not recommended normally because they'll hang the UI, and look bad to the user, but in the case you mention (closing the window), it does look like that's what you want to do anyway, so in that case it may be a good idea.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NickVdCommented:
If you are dealing directly with the XMLHttp object (or MS equivalent) you can simply pass false as the third argument in the .open() method.

I.e.
xhr.open('GET', 'http://example.com/ajax', false); //synchronous
xhr.open('GET', 'http://example.com/ajax', true[or omitted]); //asynchronous


<a href="http://developer.apple.com/internet/webcontent/xmlhttpreq.html">Great Resource!</a>
0
NickVdCommented:
doh, thought i could do a standard link :(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.