Getting the AJAX HTTP response code as 0

I have pretty simple AJAX and PHP code. When calling PHP via AJAX, it gets the response code as 0. The PHP code is running successfully, but I can not get the answer. What does this status mean “0” and how can I solve it?

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 that I used. Forgive me if I have to add something more than that. 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;
}
+3
source share
4 answers

, , , , , JS. , AJAX, AJAX jQuery.

, . JSON php , .

<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, - AJAX.

+1

readyState .

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

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

readyState 0? , , , "xmlhttp.send(null)"...

, 0, send, . , ?

+2

, HTTPS, (, ). , XML , .

0

Source: https://habr.com/ru/post/1742431/


All Articles