开发者

Receiving AJAX HTTP Response Code as 0

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");
        } el开发者_高级运维se {
            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;
}


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:

  • XMLHttpRequest - Why Status 0, and StatusText Unknown occur
  • https://developer.mozilla.org/En/Using_XMLHttpRequest#section_3


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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜