Solved

Synchronus XML Request

Posted on 2007-04-11
4
214 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
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Isisagate
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
doh, thought i could do a standard link :(
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now