Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Synchronus XML Request

Posted on 2007-04-11
Medium Priority
Last Modified: 2012-05-05
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.
Question by:netsmithcentral
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 11

Expert Comment

ID: 18892212
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.


Accepted Solution

dmagliola earned 2000 total points
ID: 18892213
Use "false" as the third parameter in the Open method of XMLHttpRequest.
Instead of:

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


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.

Expert Comment

ID: 18892321
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.

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>

Expert Comment

ID: 18892327
doh, thought i could do a standard link :(

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question