views:

255

answers:

3

I have a pretty simple AJAX and PHP code. While calling the PHP through the AJAX it receives the response code as 0. The PHP code is successfully run, but I can't get the response. What does this status '0' denote and how can I solve this?

function confirmUser(id)
{
    xmlhttp=GetXmlHttpObject();
    regid = id;
    if (xmlhttp==null)  {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="confirm.php";
    url=url+"?id="+id;
    url=url+"&a=confirm";
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            $("#txtHint" + regid).text("Awaiting confirmation");
        } else {
            alert (xmlhttp.status); //this shows '0'
        }
    };
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

Well, this is the javascript I used. Pardon me if I should've added anything more than this. Also tell me what I missed. I appreciate your help

GetXmlHttpObject function:

function GetXmlHttpObject()
{
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}
A: 

Here are the readyState codes for you.

0. Uninitialized
1. Set up, but not sent
2. Sent
3. In flight
4. Complete

(Source: http://www.stevefenton.co.uk/Content/Blog/Date/201004/Blog/AJAX-Ready-State-Codes/)

Do you get stuck constantly on a readyState of 0? If so, it means your request hasn't been sent, although I can see a line of code in your example "xmlhttp.send(null)"...

I would predict that you'll get a 0 before you call send, but after that a different status code. What happens if you wait a bit?

Sohnee
Well, I get status as 0 only once, i.e at the end; where it should have been 200.
shyam
Are you talking about the readyState or Status? The Status should be 200 when the request is complete, with a readyState of 4. I would recommend using Firebug to track the request - it will help you to diagnose any problems as it will show you the AJAX request and any response on the "Net" tab.
Sohnee
I'm talking about the status. readyState is 4 of course.And let me try firebug.
shyam
Can you show the "GetXmlHttpObject()" method, as there may be a problem in there perhaps - it would at least allow me to re-create your code to see the issue.
Sohnee
Maybe re-work your example to use something like this... http://www.w3schools.com/Ajax/ajax_database.asp
Sohnee
I've added the function as you asked.
shyam
A: 

When working with XMLHttpRequests in the past, I've found that status 0 is usually returned for locally processed files. When I saw this question, I had a bit of a hunt around and found a confirmation of this at the following pages:

Andy E
+1  A: 

I know people may not want to hear it, but this is exactly what JS frameworks are for. Why mess with all of the various browser inclinations and disasters that are custom AJAX calls when you can just do a simple AJAX call through jQuery.

Basically, you are reinventing the wheel, and for no reason. Have your php return JSON data, and embed a variable in with the success code if you need to test for that.

<script src="jquery.js"></script>
<script>
  $.get("myphp.php", { id : "yes", blah : "stuff" }, function(data) {
  if (data.success == 1) {
    alert("got data");
  } else {
    alert("didn't get data");
  }
},"json");
</script>

Boom, you now have cross-browser AJAX.

Owen Allen
thanks for the response. I'll try it out.
shyam
could u post a link with more details for using ajax thru jquery? will be really helpful..
shyam
Actually my example is a working example. Visit http://www.jquery.com for more examples, theres tons out there. The basics of the example I posted are $.get() for a GET or $.post() for a post request. The first parameter is the filename you are loading, and the contents of that file will fill the data variable of the defined callback function. The example above is if the return file will deliver JSON, but if you just leave out the ,"json", then it will return the text of the document in full.Here's a doc from IBM - http://www.ibm.com/developerworks/library/x-ajaxjquery.html.
Owen Allen