即使链接工作 JSON 也能获得 404



我正在尝试使用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();

您需要

在URL的开头指定http

request.open('GET', "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=" + apiId + "");

最新更新