这是我的代码笔的链接:http://codepen.io/dsemel/pen/VamEyE
这似乎是有问题的代码部分:
function success(position){
var WeatherKey = '6068dffce2f44535a07202457162103';
var lat = position.coords.latitude;
var long = position.coords.longitude;
var weatherUrl = "http://api.apixu.com/v1/current.json?key=" + WeatherKey +
"&q=" + lat + "," + long;
$.ajax({
url : weatherUrl,
type: 'GET',
dataType : 'json',
success : function(data) {
var city = data['location']['name'];
var tempFar = data['current']['temp_f'];
var img = data['condition'][0]['icon'];
var desc = data['condition']['text'];
$('#weatherInfo2').text(tempFar);
}
});
}
最佳答案
遇到这些错误时,请务必检查开发人员工具控制台。您的代码抛出此错误 Uncaught TypeError: Cannot read property '0' of undefined
.
condition
对象是 current
对象的一部分,因此您必须先访问 current
对象,然后才能访问 condition
对象。
关于javascript - 显示来自 ajax json 解析的数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230219/