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;
}
shyam
  • 6,681
  • 7
  • 28
  • 45

4 Answers4

3

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
  • 338,112
  • 86
  • 474
  • 445
2

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?

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    Well, I get status as 0 only once, i.e at the end; where it should have been 200. – shyam Apr 23 '10 at 10:43
  • 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. – Fenton Apr 23 '10 at 10:49
  • I'm talking about the status. readyState is 4 of course. And let me try firebug. – shyam Apr 23 '10 at 10:52
  • 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. – Fenton Apr 23 '10 at 10:57
  • Maybe re-work your example to use something like this... http://www.w3schools.com/Ajax/ajax_database.asp – Fenton Apr 23 '10 at 11:03
  • I've added the function as you asked. – shyam Apr 23 '10 at 11:05
1

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
  • 411
  • 4
  • 11
  • could u post a link with more details for using ajax thru jquery? will be really helpful.. – shyam Apr 24 '10 at 07:52
  • 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 Apr 26 '10 at 22:21
0

This can happen if you're requesting an HTTPS resource and the handshake fails (for example an invalid certificate). In particular, if you're using the XML request object from outside a browser, the error may not be obvious.

mjaggard
  • 2,389
  • 1
  • 23
  • 45