这是我的简单网页:
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="start()">
</body>
</html>
这是我的 XMLHttpRequest:
function start(){
//Name the function that will perform request
var httpRequest;
//cross browser instance
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject){ //if IE 8 and older
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.onreadystatechange = httpResult; //what to do after response
httpRequest.open("GET","http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast?pretty", true);
httpRequest.send();
function httpResult(){
if (httpRequest.readyState === 4 && httpRequest.status === 200){
alert(httpResult.responseText);
} else {
alert("problem making request");
}
}
}
当我加载页面时,它运行代码并返回“问题提出请求”警报 3 次。我没有看到 JavaScript 控制台中弹出任何错误。任何人都知道为什么我没有从请求中得到正确的响应?