XMLHttpRequest Query/permissions

S

SE

Hi all,

Apologies if this has been done before.

I am trying to do some stuff with XMLHttpRequest in mozilla but no dice. I
have finally pared everything down to the minimum to see what is happening
with this script

-----------------------------------------------------
xmlhttp = new XMLHttpRequest();
alert(xmlhttp);
// }
alert("Trying the test now");
xmlhttp.open("GET", "test.txt",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) alert("URL Exists!");
else if (xmlhttp.status==404) alert("URL doesn't exist!");
else alert("Status is "+xmlhttp.status);
}
}
xmlhttp.send(null);
alert(xmlhttp.status);
document.write("Status is " + xmlhttp.status);
xmlhttp.send(null);

--------------------------------------------------

The first alert box comes up telling me that xmlhttp is an object. The
second box telling me that it is about to try getting the file comes up,
THEN NOTHING!

I am running the script on an Apache server.

Any help gratefully received.

Thanks.

Steve
 
D

Dag Sunde

SE said:
Hi all,

Apologies if this has been done before.

I am trying to do some stuff with XMLHttpRequest in mozilla but no dice.
I
have finally pared everything down to the minimum to see what is happening
with this script

-----------------------------------------------------
xmlhttp = new XMLHttpRequest();
alert(xmlhttp);
// }
alert("Trying the test now");
xmlhttp.open("GET", "test.txt",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) alert("URL Exists!");
else if (xmlhttp.status==404) alert("URL doesn't exist!");
else alert("Status is "+xmlhttp.status);
}
}
xmlhttp.send(null);
alert(xmlhttp.status);
document.write("Status is " + xmlhttp.status);
xmlhttp.send(null);

--------------------------------------------------

The first alert box comes up telling me that xmlhttp is an object. The
second box telling me that it is about to try getting the file comes up,
THEN NOTHING!

Why the last 3 lines of your script? Especially your second .send() is
curious.
 
S

SE

Dag

Just trying to get anything to work at present. I can delete and test
however.

Steve
 
M

Martin Honnen

SE wrote:

I am trying to do some stuff with XMLHttpRequest in mozilla but no dice. I
have finally pared everything down to the minimum to see what is happening
with this script

-----------------------------------------------------
xmlhttp = new XMLHttpRequest();
alert(xmlhttp);
// }
alert("Trying the test now");
xmlhttp.open("GET", "test.txt",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) alert("URL Exists!");
else if (xmlhttp.status==404) alert("URL doesn't exist!");
else alert("Status is "+xmlhttp.status);
}
}
xmlhttp.send(null);
alert(xmlhttp.status);
document.write("Status is " + xmlhttp.status);
xmlhttp.send(null);

As already said, the last three lines make no sense, creation, open,
onreadystatechange and the first send are all fine but then the object
has to asynchronously make the HTTP request and all further processing
has to happen in the onreadystatechange event handler so after the first
call to send there should be no more code, at least no code accessing
xmlhttp, let that simply do its work then.

The document.write call after the send might even kill anything,
depening on when the whole code is called, e.g. if you do that after
page load then that document.write call would overwrite the current
document.
 
S

SE

Martin/Dag

I have killed the last three lines and made a couple of other changes and
it is working now. Thanks for your help. I will now get down to the real
programming!

Steve
 
D

Dag Sunde

SE said:
Martin/Dag

I have killed the last three lines and made a couple of other changes and
it is working now. Thanks for your help. I will now get down to the real
programming!

Good!

You can also make it cleaner/easier to read (IMO) by separating
the onreadystatechange function in a separate function:

var xmlhttp;

function getTest() {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "test.txt",true);
xmlhttp.onreadystatechange = testCallback;
xmlhttp.send(null);
}

function testCallback() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200)
alert("URL Exists!");
else if (xmlhttp.status==404)
alert("URL doesn't exist!");
else
alert("Status is "+xmlhttp.status);
}
}
 
R

Richard Cornford

Dag said:
Good!

You can also make it cleaner/easier to read (IMO) by
separating the onreadystatechange function in a separate
function:

var xmlhttp;

function getTest() {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "test.txt",true);
xmlhttp.onreadystatechange = testCallback;
xmlhttp.send(null);
}

function testCallback() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200)
alert("URL Exists!");
else if (xmlhttp.status==404)
alert("URL doesn't exist!");
else
alert("Status is "+xmlhttp.status);
}
}

It is an opinion that you won't maintain after you have thought about it
a bit.

Asynchronous XmlHttp requests may be concurrent, that is, a second may
be initiated before the completion of the first. If you use a single
globally scoped variable to hold a reference to an XmlHttp request
object then the initiation of the second request replaces the reference
to the first object. If this happens before the first request has fully
loaded its onreadystatechange handler will be reading the readyState of
the second request whenever the state of the first changes. This will
almost certainly mean that it never does whatever it was intended to do.
And in the event that coincidence does have it act (a readyState change
happens in the first object when the second object is already at
readyState 4) it will be acting upon the wrong XmlHttp request object.

Unfortunately the possibility of having the event handling function act
upon its own object through the use of the - this - keyword is rendered
non-viable by a bug in IE that prevents the correct resolution of the -
this - keyword in the context of the onreadystatechange handler.

This situation is normally coped with through the use of closures, and
so assigning an inner function to the event handler is normal and
sensible. Subject to that reference being freed after the completion of
the process to avoid circular references causing the IE memory leak
problem. But your global reference to the object leaves an un-freed
circular reference anyway (The function object refers to its scope
chain, its scope chain refers to the global object, the global object
refers to the XmlHttp request object (as the value of its xmlhttp
property), the XmlHttp request object refers back to the function object
(as the value of its onreadystatechange property)).

Richard.
 
D

Dag Sunde

Richard Cornford said:
<snipped
It is an opinion that you won't maintain after you have thought about it
a bit.

Asynchronous XmlHttp requests may be concurrent, that is, a second may
be initiated before the completion of the first. If you use a single
globally scoped variable to hold a reference to an XmlHttp request
object then the initiation of the second request replaces the reference
to the first object. If this happens before the first request has fully
loaded its onreadystatechange handler will be reading the readyState of
the second request whenever the state of the first changes. This will
almost certainly mean that it never does whatever it was intended to do.
And in the event that coincidence does have it act (a readyState change
happens in the first object when the second object is already at
readyState 4) it will be acting upon the wrong XmlHttp request object.

That all depends on the context you use it in. As a programmer, you
know very well if you have designed your code so it is possible to
trigger a concurrent call (ie.behind a button), or if it does a one-shot
cleanup procedure in 'onunload'...

So I still maintain my opinion, but I should have mentioned the pitfalls
to the OP.
Unfortunately the possibility of having the event handling function act
upon its own object through the use of the - this - keyword is rendered
non-viable by a bug in IE that prevents the correct resolution of the -
this - keyword in the context of the onreadystatechange handler.

This situation is normally coped with through the use of closures, and
so assigning an inner function to the event handler is normal and
sensible. Subject to that reference being freed after the completion of
the process to avoid circular references causing the IE memory leak
problem. But your global reference to the object leaves an un-freed
circular reference anyway (The function object refers to its scope
chain, its scope chain refers to the global object, the global object
refers to the XmlHttp request object (as the value of its xmlhttp
property), the XmlHttp request object refers back to the function object
(as the value of its onreadystatechange property)).

This is all true, and it is a pity with the ie-bug. Because without that
bug, I could have it my way in all situations without the problems you
outline above.
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top