473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

XMLHttpRequest Object: Asynchronous vs Synchronous Mode

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
5 15483
"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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: David | last post by:
Hello I'm testing the XMLHttpRequest object in Firefox and IE. The code below works well in IE and Firefox. It shows "1" when the string is a number and "0" when not. The page aspxTest.aspx only...
42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
2
by: petermichaux | last post by:
Hi, I thought it is about time I tried writing some JavaScript with XMLHttpRequest instead of just using the Yahoo! UI library. The simple page below works in both Safari and Opera but I don't...
13
by: TLaufenberg | last post by:
I'm new to Javascript programming and I've run into a bit of a snag with making an XMLHttpRequest in the Safari browser. Actually, the request doesn't work in Firefox either but only when I use a...
1
by: libsfan01 | last post by:
Hi all Can anyone explain the relationship between SOAP and XMLHttpRequest in Javascript? What actually is SOAP? and how does it relate to the process of transferring data client-side through...
3
by: perrog | last post by:
Hi! What is the expected behaviour when you send an XmlHttpRequest just before the page is about to unload? I'm sending a XmlHttpRequest on an onClick event, and I can inspect that the request...
10
by: HugeBob | last post by:
Hi All, I'm having a problem with a web app I'm writing. I have a form for which I'm going to validate one of the fields. I use AJAX to retrieve XML containing a list of valid entries. My app...
4
by: jackchang1 | last post by:
Hi, group I have a problem with IE when using XMLHttpRequest object in asynchronous mode. I have a page, and when it is closed, the onunload event handler of the page will create an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.