Help: Why does XMLHttpRequest status always returns 0
Hi everyone,
XMLHttpRequest status returns 0. I am not sure why. I use relative URL, and I am using localhost. The response page is also on the same folder. So it is in the same domain. The code is as under:-
Code:
function getLike(value)
{
var xhr;
alert(value);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xhr=new XMLHttpRequest();
alert("XMLHttpRequest1");
}
else
{// code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");
alert("Microsoft.XMLHTTP1");
}
xhr.onreadystatechange=function()
{
alert(xhr.readyState);
if (xhr.readyState==4)
{
alert(xhr.status);
if (xhr.status==200)
{
document.getElementById("responseDIV").innerHTML=xhr.responseText;
}
}
}
//alert("onreadystatechange");
var likePath = "/Moms/babyNames/";
var likeFile = likePath + "likeBabyName.php" ;
var likeURL = likeFile + '?name='+ value;
alert(likeFile);
xhr.open("GET",likeURL,true);
xhr.send();
}
likeBabyName.php has only one line as:
Code:
<?php
echo "LikedName";
?>
Response status is 0. I have looked in web for answers, but have not found any solution for my problem. If you can point to me where I am going wrong, I would be helped very much.
Looking at your work it seems OK. The response status 0 may be due to the odditie of not having a valid URL. Check you final URL path to make sure it does actually point to you file. Put into you address bar of the browser to see if it opens the php file directly.
The variables likePath, likeFile and likeURL should not cause a problem if done right but I suggest you not do that till you sort out the problem. All of it can be included into one string with the value added at the end, but better still will be to put it into the open method bypassing the variables altogether.
I do not believe the alerts should cause any problems BUT please learn to use the browsers console.log feature to display all the information that you are trying to get from the alerts. You will be surprised what information you will find when you use it
All of it can be included into one string with the value added at the end, but better still will be to put it into the open method bypassing the variables altogether.
What that means is a two step process:
1: take this
Code:
var likePath = "/Moms/babyNames/";
var likeFile = likePath + "likeBabyName.php" ;
var likeURL = likeFile + '?name='+ value;
turn it into this....
Code:
var likeURL = "/Moms/babyNames/likeBabyName.php?name=" + value;
I DO NOT KNOW you file structure so YOU have to determine if that path is correct...and that I believe is the cause of your error.... SO CHECK IT please...
Usually a status code of zero means the request failed at the very beginning. Maybe I've not understood your set up, but if the original page that is trying to access the program on your local server did not come from the server, but was invoked as, for example, "file:///whatever/whatever/zap.html" it will be considered as a cross domain request which will fail.
So, the file is in the localhost server. And I am building/testing my website in localhost. How can it be cross-domain when the everything is in localhost?
How do you activate the page that contains the javascript that makes the AJAX request? Is it also served up from the server? For example, does it start out because you've pointed your browser at "http://localhost/Moms/babyNames/index.html" ? If, on the other hand, you are activating the initial calling page by pointing your browser at the actual file, as in "file:///localhost/Moms/babyNames/index.html" this will indeed be interpreted as a cross domain request even though they are in the same directory.
Once again-- how is the initial page that makes the AJAX call invoked?
The original file that calls the javascript file is : http://localhost/Moms/babyNames/babyBoyNames.php. It is also served from the server. This file has few babynames which I click to initiate the javascript function that makes the AJAX request to like the baby name. This file is invoked when a user clicks "babyBoyNames" menu item on the website in localhost server.
Sorry--I have no idea what the problem may be. A status code of zero usually means that the network cannot be reached. Since the initial page loads I am somewhat mystified. Try this to see if it gives you any insight into what the error might be. Good luck.
Code:
function getLike(value){
var xhr;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xhr=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("GET", "/Moms/babyNames/likeBabyName.php", true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
}
Thanks grumpyOleMan and Tcobb. I was out not working for few days, so could not reply earlier.
It seems Firefox is bit slow than Google Chrome...firefox was replying status 200 once in a while...So I looked and looked...and found this dumb mistake I had ...By the time Chrome was getting status = 200, my page was reloading itself because I had <a href = " " > in the page... I am not sure if I am explaining it right...I changed it to <a href="#">.
Your replies and suggestions were so valuable to me...because of them I knew I was doing the Ajax part right, so I had to be really dumb somewhere else...thanks for putting up with me.
Bookmarks