error
我正在构建Weather-App,当我调用API时,$.getJSON
会添加我的localhost地址。
$("#getLocation").on("click", function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$("#location").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
lat = position.coords.latitude;
lon = position.coords.longitude;
api = "api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a";
console.log(api);
$.getJSON(api, function(json) {
$("#api").html(json);
});
});
}
});
答案 0 :(得分:0)
由于URL不包含//servername
,因此它被解释为当前页面路径中的相对URL。使用:
api = "//api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a";