显示来自ajax json解析的数据时出现问题
Trouble displaying data from ajax json parse
这是我的代码笔的链接: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
对象的一部分,因此在访问condition
对象之前必须访问current
对象。
更新的工作代码笔
我认为你的代码运行良好,你只是在代码中遗漏了一件小事,我纠正了这一点http://codepen.io/rahulchaturvedie/pen/RagGdR
$.ajax({
url : weatherUrl,
type: 'GET',
dataType : 'json',
success : function(data) {
console.log(data);
var city = data['location']['name'];
var tempFar = data['current']['temp_f'];
var img = data.current.condition.icon; // correct this
var desc = data.current.condition.text; // correct this
$('#weatherInfo2').text(tempFar);
}
});
相关文章:
- jQuery 和 Ajax 请求数据问题
- JSON Ajax 数据问题
- 请求.JS和节点.js数据问题
- EasyXDM传递数据问题
- Symfony2:Highchartsbundle数据问题
- Google Analtyics Embedded API - report.数据问题
- D3 JS - 未捕获的类型错误:无法读取未定义的属性“长度” - 似乎与数据问题有关
- 提前输入获取远程数据问题
- 异步加载数据问题 AngularJS 和 Firebase
- 提前输入.js远程发送之前发送帖子数据问题
- 在 HTML 结构中打印 JSON 数据问题
- Morris Donut图表没有数据问题
- 如何修复此本地数据问题?(Chrome扩展、Javascript、jQuery)
- 使用Spring MVC控制器在JQuery Datatable中呈现数据问题
- getJSON警报未返回任何内容-数据问题
- 渲染后挖空JS从视图模型中检索数据问题
- b4jjquery-websockets库发送数据问题
- Javascript数据表填充数据问题(没有返回错误)
- Angular数据问题——不知道如何排除
- 奇怪的jQuery数据()问题-帮助