AJAX - resObjekt.readyState is only 1

M

Mark Knochen

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." *
*
************************************************************************
 
D

Danny

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
 
R

Richard Cornford

Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top