examples on jibbering.com leave me unclear how XML HTTP Request object expects urls to look

J

Jake Barnes

Curious about this code snippet on jibbering.com:

xmlhttp.open("HEAD", "/faq/index.html",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)


I tried to turn this into a function:


function checkUrlValidity(url) {
if (null) {
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} else {
var xmlhttp=new XMLHttpRequest();
}

xmlhttp.open("HEAD", url, 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)
}


Two problems:

I could not figure out the right syntax for testing for ActiveXObject.

I could not figure out what the url is suppose to look like. When I try
this:

checkUrlValidity("www/monkeyclaus.org");

the script returns a 404 error, even thought that is a legit address.
So if I try this:

checkUrlValidity("http://www/monkeyclaus.org/index.php");

I get "Uncaught exception: Permission denied for 'open'".
 
R

Randy Webb

Jake Barnes said the following on 1/27/2006 2:02 AM:
Curious about this code snippet on jibbering.com:

xmlhttp.open("HEAD", "/faq/index.html",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)

That code is intended to be executed after the first blue block of code
has been executed.
I tried to turn this into a function:
function checkUrlValidity(url) {
if (null) {
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} else {
var xmlhttp=new XMLHttpRequest();
}

If the code in the first code box on that page is executed first, then
the above lines are not needed.
xmlhttp.open("HEAD", url, 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)
}


Two problems:

I could not figure out the right syntax for testing for ActiveXObject.

See Above.
I could not figure out what the url is suppose to look like. When I try
this:

checkUrlValidity("www/monkeyclaus.org");

the script returns a 404 error, even thought that is a legit address.
So if I try this:

checkUrlValidity("http://www/monkeyclaus.org/index.php");

As McKirahan pointed out, the URL should be http://www.monkey, not
www/monkey


Test Code you can tinker with:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>AJAX Test</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Windows-1251">
<script type="text/javascript">
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE
versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
function checkURL(url){
xmlhttp.open("HEAD", url,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)
}

</script>
<body>
<p>
Please enter the FULL URL to check:
<form action=""><p>
<input type="text" name="myInput">
<input type="button" value="Check URL"
onclick="checkURL(this.form.myInput.value)">
</form>
</body>
</html>
 
T

Thomas 'PointedEars' Lahn

McKirahan said:
"Jake Barnes" [...] wrote [...]
checkUrlValidity("www/monkeyclaus.org");

the script returns a 404 error, even thought that is a legit address.

"www/" prefix is not a "legit address".

Don't you mean www.monkeyclaus.org?

This is not a "legit address" (read: a URI) either. The way he uses
XMLHttpRequest, it has to be at least "http://www.monkeyclaus.org";
everything else is hoping for the UA to correct it and is error-prone.
I would use "http://www.monkeyclaus.org/" to avoid implicit server-side
redirection.


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 1/27/2006 8:22 PM:
McKirahan said:
"Jake Barnes" [...] wrote [...]
checkUrlValidity("www/monkeyclaus.org");

the script returns a 404 error, even thought that is a legit address.
"www/" prefix is not a "legit address".

Don't you mean www.monkeyclaus.org?

This is not a "legit address" (read: a URI) either. The way he uses
XMLHttpRequest, it has to be at least "http://www.monkeyclaus.org";
everything else is hoping for the UA to correct it and is error-prone.

It is not "error-prone", it is an error. Any URL given to an HTTPRequest
Object has to be a full URL, not a partial. And that includes the Protocol.
I would use "http://www.monkeyclaus.org/" to avoid implicit server-side
redirection.

That is bad advice. If the server uses server-side redirection then the
script needs the return from the redirected page, not the URL itself as
that would be useless to you.
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 1/27/2006 8:22 PM:
McKirahan said:
"Jake Barnes" [...] wrote [...]
checkUrlValidity("www/monkeyclaus.org");

the script returns a 404 error, even thought that is a legit address.
"www/" prefix is not a "legit address".

Don't you mean www.monkeyclaus.org?
This is not a "legit address" (read: a URI) either. The way he uses
XMLHttpRequest, it has to be at least "http://www.monkeyclaus.org";
everything else is hoping for the UA to correct it and is error-prone.

It is not "error-prone", it is an error. Any URL given to an HTTPRequest
Object has to be a full URL, not a partial. And that includes the
Protocol.

We are both wrong. What you say is not specified anywhere, in fact the
IXMLHttpRequest reference specifies that even relative URIs are allowed:

<URL:http://msdn.microsoft.com/library/en-us/xmlsdk/html/52aaf5ff-e302-4490-821a-cb3a085fe5ee.asp>

This is supported by the source code of nsIXHTMLHttpRequest which is the
Gecko DOM implementation of that interface.

<URL:http://lxr.mozilla.org/seamonkey/source/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp#991>

Besides, we are talking about XML_HTTP_ requests. It is perfectly
reasonable for a UA to prefix any URI reference without the scheme-part
with `http://' (and the resolved relative path-part).
That is bad advice.

Not at all.
If the server uses server-side redirection then the script needs the
return from the redirected page, not the URL itself as that would be
useless to you.

Nonsense, server-side redirection works transparent to the XMLHttpRequest
object.

However, if the trailing slash indicating the index document is omitted,
most Web servers (including all Apache HTTP servers not explicitly
configured otherwise) redirect to the URL with the trailing slash (per a
301 [Moved Permanently] response) if there is no matching resource, and
include both requests in their logs. This takes longer and increases
network and server load compared to including the slash in the request
in the first place, and is therefore not to be recommended. Example:

<URL:http://web-sniffer.net/?url=http://...Firefox/1.5+Mnenhy/0.7.3.0+Web-Sniffer/1.0.23>
(or use `HEAD -UsSe http://www.microsoft.com/ie`)

However, I was wrong in assuming that the former also applies to the index
document of the document root -- which is obvious to me now as the first
parameter of the HTTP request command (OPTIONS|HEAD|GET|POST|PUT|DELETE
TRACE|CONNECT) must include a _non-empty_ URI reference [or a `*']); the
UA will have to "append the /" in order to issue the request. So both
URIs are equal in _this_ case, indeed.

<URL:http://web-sniffer.net/?url=http://...Firefox/1.5+Mnenhy/0.7.3.0+Web-Sniffer/1.0.23>
<URL:http://web-sniffer.net/?url=http://...Firefox/1.5+Mnenhy/0.7.3.0+Web-Sniffer/1.0.23>
<URL:http://web-sniffer.net/?url=http://...Firefox/1.5+Mnenhy/0.7.3.0+Web-Sniffer/1.0.23>
<URL:http://web-sniffer.net/?url=http://...Firefox/1.5+Mnenhy/0.7.3.0+Web-Sniffer/1.0.23>


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 1/27/2006 11:05 PM:
Randy said:
Thomas 'PointedEars' Lahn said the following on 1/27/2006 8:22 PM:
McKirahan wrote:
"Jake Barnes" [...] wrote [...]
checkUrlValidity("www/monkeyclaus.org");

the script returns a 404 error, even thought that is a legit address.
"www/" prefix is not a "legit address".

Don't you mean www.monkeyclaus.org?
This is not a "legit address" (read: a URI) either. The way he uses
XMLHttpRequest, it has to be at least "http://www.monkeyclaus.org";
everything else is hoping for the UA to correct it and is error-prone.
It is not "error-prone", it is an error. Any URL given to an HTTPRequest
Object has to be a full URL, not a partial. And that includes the
Protocol.

We are both wrong.

I don't believe I am wrong and my belief in that is based on testing in
IE6 with the code I posted. When testing for http://www.microsoft.com I
get the alert that the URL Existed. When testing for www.microsoft.com I
get a status code of 2 and then a script error that the resource
couldn't be located.

That indicates, to me, that the protocol has to be there.

What you say is not specified anywhere, in fact the
IXMLHttpRequest reference specifies that even relative URIs are allowed:

<URL:http://msdn.microsoft.com/library/en-us/xmlsdk/html/52aaf5ff-e302-4490-821a-cb3a085fe5ee.asp>

When checking for a relative path, it does indeed work. But when
checking for an absolute path, the protocol must be present in IE or you
get script errors.

Even using the Msxml2.XMLHTTP.3.0 they refer to in that page it doesn't
get the proper answer in IE unless the protocol is present.

But, this is what that page says about the URL:

<quote>
The requested URL. This can be either an absolute URL, such as
"http://Myserver/Mypath/Myfile.asp", or a relative URL, such as
"../MyPath/MyFile.asp".
</quote>

How can a path be absolute without the protocol?

This is supported by the source code of nsIXHTMLHttpRequest which is the
Gecko DOM implementation of that interface.

<URL:http://lxr.mozilla.org/seamonkey/source/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp#991>

Besides, we are talking about XML_HTTP_ requests. It is perfectly
reasonable for a UA to prefix any URI reference without the scheme-part
with `http://' (and the resolved relative path-part).

The UA is not making the HTTP Request, the Script Engine is. Or maybe in
IE it is the ActiveX object making the request.
 
R

Randy Webb

Randy Webb said the following on 1/28/2006 4:10 AM:
Thomas 'PointedEars' Lahn said the following on 1/27/2006 11:05 PM:
Randy said:
Thomas 'PointedEars' Lahn said the following on 1/27/2006 8:22 PM:
McKirahan wrote:
"Jake Barnes" [...] wrote [...]
checkUrlValidity("www/monkeyclaus.org");

the script returns a 404 error, even thought that is a legit address.
"www/" prefix is not a "legit address".

Don't you mean www.monkeyclaus.org?
This is not a "legit address" (read: a URI) either. The way he uses
XMLHttpRequest, it has to be at least "http://www.monkeyclaus.org";
everything else is hoping for the UA to correct it and is error-prone.
It is not "error-prone", it is an error. Any URL given to an HTTPRequest
Object has to be a full URL, not a partial. And that includes the
Protocol.

We are both wrong.

I don't believe I am wrong and my belief in that is based on testing in
IE6 with the code I posted. When testing for http://www.microsoft.com I
get the alert that the URL Existed. When testing for www.microsoft.com I
get a status code of 2 and then a script error that the resource
couldn't be located.

That indicates, to me, that the protocol has to be there.

What you say is not specified anywhere, in fact the
IXMLHttpRequest reference specifies that even relative URIs are allowed:

<URL:http://msdn.microsoft.com/library/en-us/xmlsdk/html/52aaf5ff-e302-4490-821a-cb3a085fe5ee.asp>

When checking for a relative path, it does indeed work. But when
checking for an absolute path, the protocol must be present in IE or you
get script errors.

Even using the Msxml2.XMLHTTP.3.0 they refer to in that page it doesn't
get the proper answer in IE unless the protocol is present.

But, this is what that page says about the URL:

<quote>
The requested URL. This can be either an absolute URL, such as
"http://Myserver/Mypath/Myfile.asp", or a relative URL, such as
"../MyPath/MyFile.asp".
</quote>

How can a path be absolute without the protocol?

I hate it when I wake up thinking about something on Usenet. The reason
http://www.microsoft.com works is obviously because it's a fully
qualified URL including the Protocol.

The reason that www.microsoft.com (or any other domain name) doesn't
work without the Protocol is because the HTTPRequestObject is treating
it as a relative URL. So unless you have a folder/path in the same path
as the testing page of the same name as the domain then it won't work.

The simplest solution is to give it the fully qualified URL, including
Protocol, unless you explicitly want to use a relative path.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top