[英]Geolocation won't be displayed in console
I am using a openweather api to display the weather of the user's city. 我正在使用openweather api来显示用户所在城市的天气。 I begin by using the geolocation function to find the latitude and longitude of the user. 我首先使用地理位置功能来查找用户的纬度和经度。 That data is then passed in the variable api. 然后将该数据传递到变量api中。 The console keeps displaying a value of undefined. 控制台一直显示未定义的值。 I'm not sure where I went wrong. 我不确定哪里出了问题。
Here is the JavaScript: 这是JavaScript:
$(document).ready(function(){
var lat;
var long;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
var api='http://api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+long+'&appid=myapi';
$.getJSON(api,function(data){
var city= data.name;
console.log(api);
console.log(city);
});
});
}
});
So change the code for getJSON to this : 因此,将getJSON的代码更改为:
$.getJSON(api,{})
.done(function(data){
console.log(data.name);
})
.fail(function(err){
console.log(err.responseText);
});
and check the console for the actual error 并检查控制台的实际错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.