【发布时间】:2016-02-22 21:28:42
【问题描述】:
我正在使用 jquery 创建一个天气应用程序。我遇到的问题是在我的 getJSON 中,我想使用我使用您当前的纬度和经度坐标创建的变量。带有我的 openweather api 密钥的 url 在浏览器中加载 json 文件就好了,但它似乎不想在我的 getJSON 中工作。
这是我的 javascript 代码,用于根据您的位置显示天气信息。
var lat;
var lon;
var jsonURL;
navigator.geolocation.getCurrentPosition(GetLocation);
function GetLocation(location) {
lat = location.coords.latitude;
lon = location.coords.longitude;
jsonURL = 'api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&APPID=example&units=imperial'
}
$("#getWeather").click(function(){
console.log(jsonURL);
$.getJSON(jsonURL , function( data ) {
var items = [];
items = data; $("#weather").text(items.main.temp).append('℉');
});
});
【问题讨论】:
-
请删除任何敏感数据,例如您的
APPID。
标签: javascript jquery json api