• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in 'async' funtionality of ajax

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that's because of ajax's async functionality, but how do I get my desired result ?

Regards,
Joshua
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67746
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
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.
 
Bear Bibeault
Sheriff
Posts: 67746
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
For example, in jQuery you code would be:



It doesn't get any simpler than that.
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your inputs.

Regards,
Joshua
reply
    Bookmark Topic Watch Topic
  • New Topic