click me js code var MyObj = { startup : function...">

XMLHttpRequest.status always returns 0

HTML

<a href="#" onclick="MyObj.startup()">click me</a>

js code

var MyObj = 
{
    startup : function()
    {
        var ajax = null;
        ajax = new XMLHttpRequest();
        ajax.open('GET', 'http://www.nasa.gov', true);
        ajax.onreadystatechange = function(evt)
        {
            if(ajax.readyState == 4)
            {
                if (ajax.status == 200)
                {
                    window.dump(":)\n");
                }
                else
                {
                    window.dump(":(\n");
                }
            }
        }
        ajax.send(null);
    }
}

ajax.statusalways returns 0, no matter what site it has, no matter what the actual return code is. I say actual because it ajax.statusTextreturns the correct value like OK or Redirect ...

ajax.readyState also returns the correct values ​​and 4 at the end.

+3
source share
2 answers

, php- (xampp- URL-, ). php proxy wget url . , html ( http://localhost/your.html) ajax . , , .

+4

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


All Articles