请您帮忙看一下代码。我想将标题名称从“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 请求之前提醒状态。
希望有帮助!
关于javascript - json 无法更改 dataWithLabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143597/