473,412 Members | 5,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

AJAX - resObjekt.readyState is only 1


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
2 1971
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Eric Wallstedt | last post by:
I have a page that "logs" changes made to input fields using ajax to pass data to a cgi. I use POST and it works fine most of the time (all the time in IE). But it fails when I get the data from...
4
by: Frances | last post by:
I literally started learning AJAX just last weekend.. I have this page, http://www.francesdelrio.com/ajax/db2.html, where I'm essentially doing what's here,...
13
by: Marvin Zhang | last post by:
Hi, I'm not familiar with web programming, but I have a problem here. I have a page. When a user click one button on it, I will use AJAX to request a PHP script which will do a bunch of tasks,...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
5
by: Heofz | last post by:
Hey all, I've been banging my head against a brick wall on this one for the last 12 hours, and the time has come for me to give up and consult the gurus on this one. The below URL contains a...
3
by: nghivo | last post by:
I attempted to synchronize async Ajax calls using the following JS blocks: ==================================================== function getXMLHTTPRequest() { try { req =...
20
by: Bryan A | last post by:
Is there a way to add a timeout to this script so that it times out at a certain time. So it would be auto updating every 2seconds and it would timeout like after 100 seconds with a message?. ...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.