Solved

Synchronus XML Request

Posted on 2007-04-11
4
217 Views
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.
0
Comment
Question by:netsmithcentral
[X]
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
4 Comments
 
LVL 11

Expert Comment

by:Isisagate
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.


0
 
LVL 3

Accepted Solution

by:
dmagliola earned 500 total points
ID: 18892213
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
 
LVL 5

Expert Comment

by:NickVd
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.

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
 
LVL 5

Expert Comment

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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tools for analysis of code 4 50
How to escape unsafe HTML tags 4 36
Link not working 6 43
Html split(text) Part2 6 23
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, …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

726 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