我正在尝试制作一个页面,该页面将使用 openweathermap api 来显示用户的城市和本地温度,不幸的是它似乎没有处理 JSON api 并且什么都不做。据我所知,我的代码很好,所以我不明白哪里出了问题。
这是 jsfiddle 链接和 javscript 代码:
https://jsfiddle.net/qo2h1L9e/2/
$(document).ready(function() {
var data;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
console.log(lat);
console.log(lon);
console.log("api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=4907e373098f091c293b36b92d8f0886");
$.getJSON("api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=4907e373098f091c293b36b92d8f0886", function(json) {
data = json;
console.log(data.name);
$("#city").text(data.name);
});
});
}
});
最佳答案
我在编码时遇到了类似的问题。假设你在 FCC 上?
无论如何,尝试添加 &callback=?到 api 网址。您可能需要以 JSONP 而不是 JSON 的形式获取数据。
此外,您不需要定义数据。您可以直接通过 json 参数访问该对象。
关于Javascript 似乎没有处理 JSON api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095949/