Log in

View Full Version : httprequest problem



toktam
12-31-2005, 09:38 AM
Hi,
I have started to use Ajax.I have downloaded the following code but the status property of the httprequest is 0 and I donot know how to fix it.The server is apache Http server and having it running or not, seem does not make any difference.
<script type="text/javascript" language="javascript">

var http_request = false;

function makeRequest(url) {

http_request = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url, true);
http_request.send(null);

}

function alertContents() {


if (http_request.readyState == 4) {

if (http_request.status == 200) {
alert(http_request.responseText);
} else {
alert('There was a problem with the request.'+http_request.statusText);
}
}

}
</script>
<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('test.html')">
<p>
Make a request
</span>

Twey
12-31-2005, 02:58 PM
I daresay you're testing that page locally (/home/user/ajax.html or C:\Documents and Settings\user\ajax.html) rather than via the webserver (http://127.0.0.1/ajax.html).

jstgermain
12-31-2005, 06:02 PM
why are you trying to run it on apache if you are not using mysqp or php. just test it normally.

Twey
12-31-2005, 07:13 PM
jstgermain: The HTTP Request object, strangely, needs a server to make HTTP requests to. Apparently, on Windows, a local file is treated as a resource on a webserver. This idiosyncratic and potentially dangerous behaviour allows the XMLHttpRequest object to be used without a webserver. However, it cannot be relied upon to act the same way as it would had it a real webserver to make requests to.

jstgermain
01-01-2006, 01:28 AM
jstgermain: The HTTP Request object, strangely, needs a server to make HTTP requests to. Apparently, on Windows, a local file is treated as a resource on a webserver. This idiosyncratic and potentially dangerous behaviour allows the XMLHttpRequest object to be used without a webserver. However, it cannot be relied upon to act the same way as it would had it a real webserver to make requests to.

thanks for that insight, i was not aware that javascript needed to be run on a server for certain scripts. i thought all javascript could be run normally on the computer.

Twey
01-01-2006, 01:24 PM
Generally speaking, it can. AJAX is the exception to the rule.

berko
01-10-2006, 02:53 PM
I tried an AJAX example on my local machine (Powerbook running 10.4.3, Apache, MySQL, PHP) and couldn't get it to work either. I uploaded it to my hosting space and it didn't work there either. You can see the code at my Google groups post. (http://groups.google.com/group/ajax-world/browse_thread/thread/7af71aceabe18047) I appreciate any help you guys can offer.

Twey
01-10-2006, 04:27 PM
Could you give us the URL of the uploaded page?