[英]XMLHTTPRequest Request To Open Weather Returning Status 0
Title says pretty much the problem. 标题几乎说明了问题。 I've entered the URL into my address bar and it comes back fine.
我已经将URL输入到地址栏中,并且可以正常返回。 I feel like its something stupid I just can't see because I've been staring at it for awhile, but here's the code.
我觉得它有些愚蠢,我只是看不到,因为我已经盯着它看了一段时间,但这是代码。
(function() {
var httpRequest;
document.getElementById("weatherButton").addEventListener('click', makeRequest);
function makeRequest() {
httpRequest = new XMLHttpRequest();
if (!httpRequest) {
alert('Cannot create XMLHTTP instance!');
return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open("GET", "api.openweathermap.org/data/2.5/weather?zip=94040&APPID=xxxxxxxxx&mode=json");
httpRequest.send();
}
function alertContents() {
if (httpRequest.readyState != 4) {
return;
}
if (httpRequest.status == 200) {
alert(httpRequest.responseXML);
}
if (httpRequest.status != 200) {
alert(httpRequest.status + ": " + httpRequest.statusText_)
alert(httpRequest.readyState)
}
}
})();
Try specifying the full path: 尝试指定完整路径:
httpRequest.open("GET", "https://api.openweathermap.org/data/2.5/weather?zip=94040&APPID=xxxxxxxxx&mode=json");
Otherwise, it looks for [yourdomain]/api.openweathermap.org/...
. 否则,它将查找
[yourdomain]/api.openweathermap.org/...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.