Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

What is "asynchronous" in AJAX?

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone:



The "asynchronous" in AJAX implies that the processing of the web page continues while XMLHttpRequest is processed on the server and a response is returned to the browser client.



Huh??? Could you explain?
[ December 09, 2007: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 67602
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you make an Ajax request, the JavaScript code following it and any other browser activities continue as if nothing had happened, When the response of the request is returned, a callback function then executes. Since this callback executes outside the normal flow of the code, and at a non-deterministic point in time (whenever the server returns the response) it is termed asynchronous.

You can make synchronous Ajax calls, but that's a very rare thing to do.
[ December 09, 2007: Message edited by: Bear Bibeault ]
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!

It often looks like I can make millions of AJAX requests in a very short time.... will the server process all of them? While printing the results in the web page, some of the results of later requests will be overridden by results of earlier requests?
Looks like a good way to kill a server?
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The application is only as smart as you code it. So if you do not code for requests coming out of order, than it will show up out of order.

If you were to read up on the XMLhttpRequest object, you would find out information on the number of open requests allowed at one time. There is plenty of information and books out there onn the subject.

Eric
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a bit of a hurdle going from 1 concurrent xhr request to 2. (From 2 to [n]n[/n] is just more of the same.) You tell xhr what function to call with onreadystatechange. If you have multiple instances of xhr calling that function, you have to keep track of which response goes with which request. I'm not a skilled scripter, but here's what I wound up with after Googling about:
[/code]
function callXmlRequest( uri, callback, onError )
{
try
{
var xhr = getXMLHttpRequest( )
function callbackClosure(){
if (xhr.readyState == 4)
{
if (xhr.status == 200)
callback( xhr.responseText )
else
alert("Error calling server: ...");
}
}
xhr.open( "GET", uri, true );
xhr.onreadystatechange = callbackClosure;
xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
xhr.send( );
}
catch( e )
{
if( onError )
onError( e )
}
}
[/code]
It gets a new xhr every time though and gives the onreadystatechange method a reference to xhr (by being a closure). BTW: My code worked fine in FireFox before I did this bit of trickery, but not at all in IE.

Bear and the gang usually jump in here to recommend using a proven library for all this. I agree completely, but am not allowed to introduce open source libraries without an approval process.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It really is not that bad to go from one request to two if you use OO JavaScript to handle multiple requests and queuing. If you can not use Open Source, than I would suggest looking at the open source project does for the XHR request and copy it. You really do not need all the code.

Eric
reply
    Bookmark Topic Watch Topic
  • New Topic