Why does my xmlhttp.status return zero?

Asked

Viewed 73 times

0

I got a problem in my , I try to catch a json, but the xmlhttp.status is always 0(zero). I researched about it, but found nothing very concrete.

Below, I simplified the code to highlight the problem:

<!DOCTYPE html>
<html>
<body>
<h1>Meu Projeto</h1>

<button onclick="math()">Current Math</button>
<div id="teste"></div>
<div id="status"></div>
<div id="readystate"></div>
<script>
identificador = 0;

function math(){
  
var url = "https://br.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/BR1/974721?api_key=5";
  
document.getElementById("teste").innerHTML = "URL: " + url;
var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", url, true);
  xmlhttp.send();
  xmlhttp.onreadystatechange = function () {

document.getElementById("status").innerHTML = "Status: " + xmlhttp.status;
document.getElementById("readystate").innerHTML = "ReadyState: " + xmlhttp.readyState;
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  	  alert("OK tudo certo");
    }

    if (xmlhttp.readyState == 4 && xmlhttp.status == 404) {
  	  alert("Erro 404");
    }
    if (xmlhttp.readyState == 4 && xmlhttp.status == 0) {
  	  alert("status 0????");
    }
  }
}

</script>
</body>
</html>

  • Have you noticed that this does not occur in IE?

  • You use which programming language?

  • In IE is working.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.