Solved

Synchronus XML Request

Posted on 2007-04-11
4
220 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

631 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