Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest.status always returning 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.status always returning 0, no matter which site it is, no matter what is the actual return code. I say actual, because ajax.statusText returning correct value, eg OK or Redirecting...

ajax.readyState also returns proper values and 4 at the end.

like image 685
Pablo Avatar asked Jun 13 '26 19:06

Pablo


1 Answers

You can overcome this easily in a local environment by setting up a php proxy (xampp a server and pass a querystring for the url you want to grab). Have your php proxy wget the url and echo its contents. That way your local html file (when viewed as http://localhost/your.html) can send ajax requests out of domain all day. Just don't expect the content to work as though it were local to that domain.

like image 149
Gabriel Avatar answered Jun 16 '26 09:06

Gabriel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!