$.getJSON 添加 'http://localhost:3000' 到 api URL
$.getJSON adds ' http://localhost:3000' to api URL
error
我正在构建 Weather-App,当我调用 API 时,$.getJSON
添加了我的本地主机地址。
GET
http://localhost:3000/api.openweathermap.org/data/2.5/weather?lat=50.064650099999994&lon=19.9449799&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a
404 (Not Found)
$("#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);
});
});
}
});
因为 URL 不包含 //servername
,它被解释为当前页面路径的相对 URL。使用:
api = "//api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a";
error
我正在构建 Weather-App,当我调用 API 时,$.getJSON
添加了我的本地主机地址。
GET http://localhost:3000/api.openweathermap.org/data/2.5/weather?lat=50.064650099999994&lon=19.9449799&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a 404 (Not Found)
$("#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);
});
});
}
});
因为 URL 不包含 //servername
,它被解释为当前页面路径的相对 URL。使用:
api = "//api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a";