我尝试使用OpenWeatherMap并获取天气的JSON。 在控制台中我看到404消息,但是当我手动访问URL时它会给我正确的JSON
var latitude;
var longitude;
var apiId = "c440e3f473378f9705827ed71efe5dcc";
var request = new XMLHttpRequest();
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function getPosition(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
getJson();
});
} else {
alert('Geo location not working or not supported by your browser.');
}
}
function getJson() {
request.open('GET', "api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=" + apiId + "");
request.onload = function(data) {
console.log(data);
};
request.send();
}
getLocation();

答案 0 :(得分:2)
您需要在网址开头指定http
request.open('GET', "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=" + apiId + "");