• 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

AJAX class problem

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
basically I was trying to create a class that I could just pass a URL and then call an execute function and then a get for the responseText, but I seem to have a problem, here is my code:



when I call Execute then the get method, the return value is empty.
I think what's happening is when I do:



that it's calling the obj.getResponseText before the readystate of the ajaxRequest in the Execute is at 4.
is there any code you can put in to "join" the ajax call and then continue? Or is that even the problem?

Thanks,

Justin
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your getResponseText is calling "this", which is a different "this" than it is in your function.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've changed the code to this:



when I call the getResponseText function I get the "wtf?!" text, but not "wtf?! +responseText+wtf?! ", which let's me know the ajaxRequest.readystate isn't changing fast enough for the get function to catch the appended text.

How can I get around this problem?

Thanks,

Justin
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As already stated

this.responseT = ajaxRequest.responseText;

is NOT in the same scope.

get firebug, set a breakpoint, and inspect what this is.

Eric


 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I understand that this.responseT is in the ajaxRequest object's scope now, because it is the 'owner' of the readystatechanged function, but how to I get the responsetext to return.

I've tried just putting a "class" scope variable 'var responseT = 'wtf?!';'

and then in the readystate == 4 conditional put 'responseT = ajaxRequest.responseText;'

and in the get 'return responseT;'

but it still only returns 'wtf?!'..

So what's the deal?

Thanks again,

Justin
 
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
You are calling it before the response comes back. You are using an asynchronous request, you are treating it like a synchronous call.

You really should look at a library to make Ajax calls.

Eric

 
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

Eric Pascarello wrote:You really should look at a library to make Ajax calls.


Agreed. If you are doing this as an academic exercise, that's fine.

But if this is for production code, it's madness in this day and age to do Ajax "by hand" -- there are just too many nuances and cross-browser issues that the libraries already handle for you.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I guess I'll say it is an academic exercise so I can get some help...
How can I get the function to return it within the time I call getResponseText()?

I've tried a while loop like so:



but the loop is endless, I figured that when the readystate == 4 finally happens that it would
set the responseT object and then when I called getResponseText() after that the loop would break?

Thanks,

Justin
 
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
Using a loop is insane.

And to be honest, I'm having a hard time getting a handle on your issue because your posted code indentation is rather wonky, so it's hard to see the structure (which, as things go, is really much more important in JavaScript than a language like Java). Perhaps you could clean up the code and repost?

What's your beef with using a library like the "big boys" do?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol i don't have any beef, just want to know how to get it to work... want to learn.

I can clean it up yeah, I'll post it in a min.

The problem is when I execute the AJAXLib.Execute() method, that method inturn calls
the ajaxRequest object's .open() and .send() method.

And then when I call my AJAXLib.getResponseText(), the ajaxRequest object's readystate hasn't
hit 4 yet, so my responseT object never get's set to ajaxRequest.responseText;

Let me clean up the code real quick...

Thanks,

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code I'm using to test the class is like so:

 
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
So it is an academic exercise.

The problem is that you are trying to get the responseText serially in line 7 of your test code. No can do.

You must wait until the readyState indicates that it's ready.

So you need to set things up so that you can pass a callback function so you can essentially say "Here, execute this when things are ready".

Otherwise, you are doomed to failure.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont know how to do a callback function so I just renamed the Execute function to updateElementHTML(id)
and just did 'document.getElementById(id).innerHTML = ajaxRequest.responseText;'.

A lot easier and it works lol.

Thanks for the information though.

Justin

Oh wait, so i can essenctially do like



how would I call that function though? like:



Like that kind of?

Thanks again,

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
awesome it worked lol! not as independent as i would like but good nuff.



Thanks,

Justin
 
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
How much harder is it to pass the function reference rather than hard-doing it?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, but I don't really understand what you mean.
I did pass the function reference, but the function has to be defined if i want something to be done with
the responseText right?

Thanks,

Justin
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic