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

XMLHttpRequest Query/permissions

P: n/a
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

Sep 8 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"SE" <st***@ab.com> wrote in message
news:uG****************@fe2.news.blueyonder.co.uk. ..
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.

--
Dag.
Sep 8 '05 #2

P: n/a
SE
Dag

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

Steve
"Dag Sunde" <me@dagsunde.com> wrote in message
news:T5*******************@juliett.dax.net...
"SE" <st***@ab.com> wrote in message
news:uG****************@fe2.news.blueyonder.co.uk. ..
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.

--
Dag.

Sep 8 '05 #3

P: n/a


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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 8 '05 #4

P: n/a
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
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:43***********************@newsread4.arcor-online.net...


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.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Sep 8 '05 #5

P: n/a
"SE" <st***@ab.com> wrote in message
news:Wv*****************@fe2.news.blueyonder.co.uk ...
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);
}
}

--
Dag.

Sep 8 '05 #6

P: n/a
Dag Sunde wrote:
SE wrote:
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);
}
}


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.
Sep 9 '05 #7

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:df*******************@news.demon.co.uk...
Dag Sunde wrote:
SE wrote:
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:

<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.

--
Dag.
Sep 9 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.