jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes problem in 'async' funtionality of ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem in Watch "problem in New topic
Author

problem in 'async' funtionality of ajax

Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
I am trying to build a sample web application using Ajax.

Currently I have 3 components in the web server:
1. a html file (A.html)
2. a java script (A.js)
3. a JSP (A.js)

I also have MYSQL as my backend.

The entries in the DB will be modified through some other web application.

The goal of the application is that once A.html is requested, it shall request
A.jsp using Ajax (XMLHttpRequest) and then draw the response into a graph.

Part of A.html has code like:



displayInBarGraph function is present in A.js and looks like below:



showBarGraph method is also present in A.js and is as below:



The problem that I am facing is that I get alert number 2 before alert number 1, why is it so?

I need to set the hidden input type value of A.html from the location in which alert number 1 is called and use that value from the location in which alert number 2 is called.

Regards,
Joshua


SCJP,SCWCD, Into ATG now!
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
I think that's because of ajax's async functionality, but how do I get my desired result ?

Regards,
Joshua
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Because Asynch means it goes off and does things and does not wait. That is how it works.

It calls the function and keeps on chugging away. If the other one is supposed to hapen after the request, than you need to call it from your onreadystatechange functionality. When you do that, you can not use document.write.


Eric
[ December 07, 2007: Message edited by: Eric Pascarello ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

You'd be far better off adopting jQuery or Prototype to help you with your Ajax. Either makes what you are trying to do brain-dead simple.

At this point in time, writing "raw" Ajax makes little sense.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

For example, in jQuery you code would be:



It doesn't get any simpler than that.
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
Thanks all for your inputs.

Regards,
Joshua
 
 
subject: problem in 'async' funtionality of ajax
 
Similar Threads
Ajax Real Time Example
Problem with UTF-8 Character encoding
Getting Values from a window to parent page
Simple Ajax example doesn't work
Can't get first example to work