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

Help with responseText from XMLHttpRequest

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a javascript object called Ajax; this object has the retrieve function that returns the responseText from the XMLHttpRequest object.
I have a user javascript that creates the Ajax object and calls the retrieve function. I am not able to get the responseText back from the retrieve function; it returns "underfined" all the times.
Can someone please help me with this. Thanks in advance.

Ajax object:
============


[edit - Added code tags]
[ September 23, 2008: Message edited by: 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
The issue you have is you are acting like the call you are making is synchronous, but you are using an asynchronous call.

You function goes off on its own and you place where you called it keeps running. It does not wait for the response [hence why it is undefined].

Design your code so it makes the call and onreadystatechange, you call the next step with the value.

Eric
 
Bob Green
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric,
Can you please elaborate a little bit on your statement
"Design your code so it makes the call and onreadystatechange, you call the next step with the value."

I don't quite understand.

Thanks,
 
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
Well it really depends on what you are doing but the basic idea is




If you use a framework to do it, it is pretty simple.

JQuery Example:


Eric
 
Bob Green
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric, Sorry I still don't get your idea.
 
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
Break up your code into two chunks

Part before Ajax call
Part after Ajax call

Call the second part from the Ajax call.



There are better wasy to do it [not hard coding function calls], libraries enable it if you do not want to code it all.

Eric
 
Sheriff
Posts: 67752
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
In an asynchronous request, the response is not available until the request completes. You are attempting to grab the data synchronously before it is ready.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic