我需要在没有jQuery的情况下获取JSON页面数据。我的代码适用于Chrome和IE,但在Mozzila上不起作用。为什么?
<script type="text/javascript">
function loadJSON(path, success, error){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
if (success)
success(JSON.parse(xhr.responseText));
} else {
if (error)
error(xhr);
}
}
};
xhr.open("GET", path, true);
xhr.send();
}
loadJSON(
'https://freegeoip.net/json/',
function(data) { alert(data.ip); },
function(xhr) { alert("BAD"); }
);
</script>
它在firefox上显示警报("糟糕"),但是在Chrome上显示警报IP