*
The moose likes HTML, CSS and JavaScript and the fly likes AJAX class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX class problem" Watch "AJAX class problem" New topic
Author

AJAX class problem

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your getResponseText is calling "this", which is a different "this" than it is in your function.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 24, 2006
Posts: 802
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
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jan 24, 2006
Posts: 802
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
code I'm using to test the class is like so:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jan 24, 2006
Posts: 802
awesome it worked lol! not as independent as i would like but good nuff.



Thanks,

Justin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

How much harder is it to pass the function reference rather than hard-doing it?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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 sorta covered in the JavaRanch Style Guide.
 
subject: AJAX class problem
 
Similar Threads
Ajax and Firefox
IE 6 problem with Dates?
AJAX problem
Do AJAX calls not work everytime?
Need help with Auto Complete Script