By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

XMLHttpRequest Object: Asynchronous vs Synchronous Mode

P: n/a
Hi All,

I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?

Mar 29 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"HugeBob" <rn****@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,

I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?
http://www.w3.org/TR/XMLHttpRequest/#dfn-send

The ramifications of not using the asynchronous aspect of AJAX is having the UA or browser
block activity until the request returns, finishes, loads, whatever.

I would not say that the open() and send() methods continue. The onreadystatechange
property still receives a dispatch however (even when blocking). So instead of being
notified of what open() and send() are doing you will receive an onload event and the
readyState of 4.

I would say you are incorrect in thinking that it cannot update its interface during a
synchronous call (assuming you really meant onreadystatechange instead of open() and
send()).

I hope I worded all that correctly.

-Lost
Mar 29 '07 #2

P: n/a
On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.netwrote:
"HugeBob" <rnu...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,
I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?

http://www.w3.org/TR/XMLHttpRequest/#dfn-send

The ramifications of not using the asynchronous aspect of AJAX is having the UA or browser
block activity until the request returns, finishes, loads, whatever.

I would not say that the open() and send() methods continue. The onreadystatechange
property still receives a dispatch however (even when blocking). So instead of being
notified of what open() and send() are doing you will receive an onload event and the
readyState of 4.

I would say you are incorrect in thinking that it cannot update its interface during a
synchronous call (assuming you really meant onreadystatechange instead of open() and
send()).

I hope I worded all that correctly.

-Lost
I find myself in the same state as you signature, LOL. Well, sort
of. I've got an app that uses AJAX. The onreadystatechange method
sets a global variable with XML it retrieves from the server when
readyState is 4. The global variable is later searched via JS. The
application works in IE when synchronous mode is used and not at all
in FireFox 1.5 (the onreadystatechange method never seems to run).
So, take the following function:

globalVariable = null;
function ajaxFunction( )
{
var xmlHttp;

xmlHttp = getXMLHTTPRequestObject( );
alert(xmlHttp);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
globalVariable = xmlHttp.responseText;
}
}

xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}

So, are you saying that even when this function completes,
globalVariable may not have anything in it when it's accessed later
regardless of whether I use asynch or synch mode?

Mar 29 '07 #3

P: n/a
"HugeBob" <rn****@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.netwrote:
>"HugeBob" <rnu...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googleg roups.com...
Hi All,
I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?

http://www.w3.org/TR/XMLHttpRequest/#dfn-send

The ramifications of not using the asynchronous aspect of AJAX is having the UA or
browser
block activity until the request returns, finishes, loads, whatever.

I would not say that the open() and send() methods continue. The onreadystatechange
property still receives a dispatch however (even when blocking). So instead of being
notified of what open() and send() are doing you will receive an onload event and the
readyState of 4.

I would say you are incorrect in thinking that it cannot update its interface during a
synchronous call (assuming you really meant onreadystatechange instead of open() and
send()).

I hope I worded all that correctly.

-Lost

I find myself in the same state as you signature, LOL. Well, sort
of. I've got an app that uses AJAX. The onreadystatechange method
sets a global variable with XML it retrieves from the server when
readyState is 4. The global variable is later searched via JS. The
application works in IE when synchronous mode is used and not at all
in FireFox 1.5 (the onreadystatechange method never seems to run).
So, take the following function:

globalVariable = null;
function ajaxFunction( )
{
var xmlHttp;

xmlHttp = getXMLHTTPRequestObject( );
alert(xmlHttp);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
globalVariable = xmlHttp.responseText;
}
}

xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}

So, are you saying that even when this function completes,
globalVariable may not have anything in it when it's accessed later
regardless of whether I use asynch or synch mode?
No, globalVariable will have the responseText specified by the XMLHttpRequest. However,
if asynchronous is set to false, depending on the size of the server's output (e.g. how
long it takes to load) globalVariable will still have the responseText, it may not be
available as quickly.

Assuming of course that getXMLHTTPRequestObject() returns the proper XMLHttpRequest.

To support the greatest number of possible browsers first have a look at:

http://jibbering.com/2002/4/httprequest.html

....the implementation there is fairly solid.

-Lost
Mar 29 '07 #4

P: n/a
On Mar 29, 12:21 pm, "-Lost" <missed-s...@comcast.netwrote:
"HugeBob" <rnu...@gmail.comwrote in message

news:11**********************@d57g2000hsg.googlegr oups.com...
On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.netwrote:
"HugeBob" <rnu...@gmail.comwrote in message
>news:11**********************@l77g2000hsb.googleg roups.com...
Hi All,
I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?
>http://www.w3.org/TR/XMLHttpRequest/#dfn-send
The ramifications of not using the asynchronous aspect of AJAX is having the UA or
browser
block activity until the request returns, finishes, loads, whatever.
I would not say that the open() and send() methods continue. The onreadystatechange
property still receives a dispatch however (even when blocking). So instead of being
notified of what open() and send() are doing you will receive an onload event and the
readyState of 4.
I would say you are incorrect in thinking that it cannot update its interface during a
synchronous call (assuming you really meant onreadystatechange instead of open() and
send()).
I hope I worded all that correctly.
-Lost
I find myself in the same state as you signature, LOL. Well, sort
of. I've got an app that uses AJAX. The onreadystatechange method
sets a global variable with XML it retrieves from the server when
readyState is 4. The global variable is later searched via JS. The
application works in IE when synchronous mode is used and not at all
in FireFox 1.5 (the onreadystatechange method never seems to run).
So, take the following function:
globalVariable = null;
function ajaxFunction( )
{
var xmlHttp;
xmlHttp = getXMLHTTPRequestObject( );
alert(xmlHttp);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
globalVariable = xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}
So, are you saying that even when this function completes,
globalVariable may not have anything in it when it's accessed later
regardless of whether I use asynch or synch mode?

No, globalVariable will have the responseText specified by the XMLHttpRequest. However,
if asynchronous is set to false, depending on the size of the server's output (e.g. how
long it takes to load) globalVariable will still have the responseText, it may not be
available as quickly.

Assuming of course that getXMLHTTPRequestObject() returns the proper XMLHttpRequest.

To support the greatest number of possible browsers first have a look at:

http://jibbering.com/2002/4/httprequest.html

...the implementation there is fairly solid.

-Lost
Oh, my getXMLHTTPRequestObject() method handles that. I borrowed it
from W3Schools (http://www.w3schools.com/ajax/ajax_server.asp):

function getXMLHTTPRequestObject( )
{
var xmlHttp = null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
}
}
}
return xmlHttp;
}

Though, I'm still confused as to why the onreadystatechange method
isn't being called in Firefox. Excuse me for being so dense. I'm
trying to wrap my brain around this. So, basically, if I reach the
portion of my JS that wants to parse the retrieved XML before
xmlHttp.readyState==4, I'm hosed? I was thinking that using
synchronous mode would avoid this.

Mar 29 '07 #5

P: n/a
"HugeBob" <rn****@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Mar 29, 12:21 pm, "-Lost" <missed-s...@comcast.netwrote:
>"HugeBob" <rnu...@gmail.comwrote in message

news:11**********************@d57g2000hsg.googleg roups.com...
On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.netwrote:
"HugeBob" <rnu...@gmail.comwrote in message
>>news:11**********************@l77g2000hsb.google groups.com...
Hi All,
I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?
>>http://www.w3.org/TR/XMLHttpRequest/#dfn-send
>The ramifications of not using the asynchronous aspect of AJAX is having the UA or
browser
block activity until the request returns, finishes, loads, whatever.
>I would not say that the open() and send() methods continue. The onreadystatechange
property still receives a dispatch however (even when blocking). So instead of
being
notified of what open() and send() are doing you will receive an onload event and
the
readyState of 4.
>I would say you are incorrect in thinking that it cannot update its interface during
a
synchronous call (assuming you really meant onreadystatechange instead of open() and
send()).
>I hope I worded all that correctly.
>-Lost
I find myself in the same state as you signature, LOL. Well, sort
of. I've got an app that uses AJAX. The onreadystatechange method
sets a global variable with XML it retrieves from the server when
readyState is 4. The global variable is later searched via JS. The
application works in IE when synchronous mode is used and not at all
in FireFox 1.5 (the onreadystatechange method never seems to run).
So, take the following function:
globalVariable = null;
function ajaxFunction( )
{
var xmlHttp;
xmlHttp = getXMLHTTPRequestObject( );
alert(xmlHttp);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
globalVariable = xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}
So, are you saying that even when this function completes,
globalVariable may not have anything in it when it's accessed later
regardless of whether I use asynch or synch mode?

No, globalVariable will have the responseText specified by the XMLHttpRequest.
However,
if asynchronous is set to false, depending on the size of the server's output (e.g. how
long it takes to load) globalVariable will still have the responseText, it may not be
available as quickly.

Assuming of course that getXMLHTTPRequestObject() returns the proper XMLHttpRequest.

To support the greatest number of possible browsers first have a look at:

http://jibbering.com/2002/4/httprequest.html

...the implementation there is fairly solid.

-Lost

Oh, my getXMLHTTPRequestObject() method handles that. I borrowed it
from W3Schools (http://www.w3schools.com/ajax/ajax_server.asp):
<snip w3schools edited AJAX function>
Though, I'm still confused as to why the onreadystatechange method
isn't being called in Firefox. Excuse me for being so dense. I'm
trying to wrap my brain around this. So, basically, if I reach the
portion of my JS that wants to parse the retrieved XML before
xmlHttp.readyState==4, I'm hosed? I was thinking that using
synchronous mode would avoid this.
Bearing in mind a flawless (near flawless) XHR implementation will not suffer this error.

Could you perhaps offer a page of your implementation for further debugging?

-Lost
Mar 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.