Receiving AJAX HTTP Response Code as 0

自古美人都是妖i 提交于 2019-12-23 18:43:06

问题


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;
}

回答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.




回答2:


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



回答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?




回答4:


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.



来源:https://stackoverflow.com/questions/2697686/receiving-ajax-http-response-code-as-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!