By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

AJAX - resObjekt.readyState is only 1

P: n/a

Hi, i have a little problem with ajax.

The follow functions are in my site:

function sndReq(ID,divID)
{
resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);
resObjekt.onreadystatechange = handleResponse(divID);
resObjekt.send(null);
}

function handleResponse(divID)
{
if(resObjekt.readyState == 4)
{
document.getElementById(divID).innerHTML =
resObjekt.responseText;
}
}
The Variables ID and divID are in the Script - i can see
the ID and divID with alert(ID) and alert(divID).

But the resObjekt.readyState ist not 4 - only 1 ...

What is the problem?

Thanks

Mark


--
www.zeitfuerwahrheit.de

************************************************** **********************
*
Beim großen Manual, ich habe gesprochen! *
*
************************************************** **********************
*
"Ich habe Dinge gesehen, die ihr Menschen niemals glauben würdet. *
Gigantische Schiffe, die brannten, draußen vor der Schulter des Orion. *
Und ich habe C-Beams gesehen, glitzernd im Dunkel, *
nahe dem Thannhäuser-Tor. *
All diese Momente werden verloren sein... in der Zeit, *
so wie ... Tränen im Regen." *
*
************************************************** **********************
Nov 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi, i have a little problem with ajax.

The follow functions are in my site:

function sndReq(ID,divID)
{
resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);
resObjekt.onreadystatechange = handleResponse(divID);
############## Replace above line with #######################
resObjekt.onreadystatechange = function(
{handleResponse(resObjekt,divID);} // handler takes no args
##################################################
resObjekt.send(null);
}
################# and add it on the handler's implementation #################
function handleResponse(resObjekt, divID)
{
if(resObjekt.readyState == 4)
{

Danny
Nov 20 '06 #2

P: n/a
Mark Knochen wrote:
Hi, i have a little problem with ajax.

The follow functions are in my site:

function sndReq(ID,divID)
{
resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);
resObjekt.onreadystatechange = handleResponse(divID);
resObjekt.send(null);
}

function handleResponse(divID)
{
if(resObjekt.readyState == 4)
{
document.getElementById(divID).innerHTML =
resObjekt.responseText;
}
}
The Variables ID and divID are in the Script - i can see
the ID and divID with alert(ID) and alert(divID).

But the resObjekt.readyState ist not 4 - only 1 ...

What is the problem?
The code - handleResponse(divID) - is a function call. That is, the
Identifier - handleResponse - is resolved to a value that is a reference
to a function object and the - (divID) - is an ArgurmentsList, and acts
like an operator on the reference to the function object, calling the
function and passing the value of - divID - as an argument to that
function call. The function executes at this point and as at this point
the xml http request object's - send - method has not been called the
readyState of that objet will never be 4 (though it may not be 1 on all
implementations).

Having called the - handleResponse - function it is the value returned
from that function call that is assigned to the - onreadystatechange -
property of the xml http request object. The function has no explicit
return value and so returns the undefined value by default. this action
may produce an error with some (particularly ActiveX) xml http request
objects and will certainly be useless where it does not produce an error
(as when the value assigned to - onreadystatechange - is not a reference
to a function object no action will be taken in response to readyState
changes).

As the correct value to assigned to the - onreadystatechange - property
of an xml http request object is a function reference it would make more
sense to write:-

resObjekt.onreadystatechange = handleResponse;

- but that will not help as your - handleResponse - is expecting, and
using, a - divID - formal parameter and it will not get a this value
passed in. You could use a global variable as a means of referring to -
divID -, but that is a potentially catastrophic thing to do if - didID -
is expected to have differing values at different times and you use (as
you should) asynchronous xml http requests (the global value may not
have the same value at the point of making the request as it will have
at the point of receiving the response). Though you have that problem
anyway because you are using a global reference to the xml http request
object.

Closures represent the best approach - onreadystatechange - handlers:-

<URL: http://jibbering.com/faq/faq_notes/closures.html >

- where you would either have your - handleResponse - receive the -
divID - argument and itself return a reference to an inner function
object which would then be assigned to the - onreadystatechange -
handler, or you would directly assign a reference to (the object
resulting from the evaluation of) an inner function expression to the -
onreadystatechange - property. Possibly something like:-

resObjekt.onreadystatechange = function(){handleResponse(divID);};

However, (and you are not going to like this) if you are in a position
where you do not know the difference between a function call and
assigning a reference to a function I don't think you should be messing
about with xml http request objects at all. I don't see how you could
asses the suitability of that technology for any task in any contexts,
design the system, or handle the issues inherent in the technology
(mostly related to sensibly handling multiple asynchronous execution
triggering sources (user input events and http responses)) without first
being able to author in the language that it depends upon (at the
absolute minimum), and getting the design decisions wrong, or falling to
properly handle the issues, can significantly restrict the potential of
end result.

Richard.
Nov 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.