json不能't使用标签更改数据
json couldn't change the dataWithLabels
请帮助查看代码。我想将标题名称从"Jason"更改为JSON文件中写入的温度。
var dataWithLabels = [
{
"title":"Jason",
}];
$.ajax({
url: "http://api.wunderground.com/api/2b38dcff93aa3dff/conditions/q/CA/Santa_Clara.json",
type: "GET",
dataType: "json",
success: function(data) {
for(var i=0;i<1/*dataWithLabels.length*/;i++){
var statistic = data.current_observation;
dataWithLabels[i]['title']= statistic.temp_c;}}
//wanted to change Jason to the temperature written at the JSON file.Please help.
});
alert(dataWithLabels[0]['title']);
http://codepen.io/wtang2/pen/bEGQKP
这不是重复的,我正在尝试将JSON文件中的结果替换为dataWithLabels对象的标题
由于我不知道,如果你请求的JSON是正确的,我只是假设它是正确的。不过,如果你想看看在Ajax请求之后dataWithLabels
中发生了什么,你需要稍微重写一下函数:
var dataWithLabels = [{
"title": "Jason",
}];
$.ajax({
url: "http://api.wunderground.com/api/2b38dcff93aa3dff/conditions/q/CA/Santa_Clara.json",
type: "GET",
dataType: "json",
success: function(data) {
for (var i = 0; i < 1 /*dataWithLabels.length*/ ; i++) {
var statistic = data.current_observation;
dataWithLabels[i]['title'] = statistic.temp_c;
alert(dataWithLabels[i]['title']);
}
}
//wanted to change Jason to the temperature written at the JSON file.Please help.
});
现在,在您插入statistic.temp_c
之后,会提醒dataWithLabels
的状态。您的代码总是在Ajax请求之前提醒状态。
希望能有所帮助!
这是因为AJAX请求是异步工作的,所以只有在AJAX请求完成后才需要提醒结果,这样才能进行
var dataWithLabels = [{
"title": "Jason",
}];
$.ajax({
url: "http://api.wunderground.com/api/2b38dcff93aa3dff/conditions/q/CA/Santa_Clara.json",
type: "GET",
dataType: "json",
success: function(data) {
for (var i = 0; i < 1 /*dataWithLabels.length*/ ; i++) {
var statistic = data.current_observation;
dataWithLabels[i]['title'] = statistic.temp_c;
alert(dataWithLabels[i]['title']); // Alert only after data is received
}
}
});
相关文章:
- 如何使用jquery解析具有相同标签名称的yahoo天气api数据
- 查找所有TD标签并读取其数据属性
- 使用json数据更改视频标签中的src属性
- 我可以按特定的顺序迭代一个标签中的不同数据标签吗
- 如何将数据标签与数据一起传递,以便在高图中的工具提示中显示
- Highcharts车速表,里程计数器的数据标签
- 如何从标签数据类型=“;无线电”;
- 高图表数据标签重叠
- 删除高图表数据标签上的阴影/背景发光
- jQuery:仅显示新的JSON数据,并使用自己的段落标签进行迭代
- asp.net 数据列表中的跟踪标签 ID
- 如何将具有多个标签的多个的所有数据获取到一个数组中
- 数据ng在Angular中用不同的标签重复
- .scrollInto查看带有数据标记的任何元素,或使用类对带有数据标签的内容进行分段
- 半圆形圆环饼图,饼图上有标签(数据名称)和年龄百分比..和鼠标悬停上的数据编号
- 通过html标签数据属性动态加载js、css文件
- 如何在dc.js中为多标签数据制作行图
- Bootstrap 3标签数据只显示在第一个标签
- AngularJS中每个标签的唯一标签数据
- D3.JS:为我的标签数据结构实现强制导向图