【发布时间】:2015-10-22 07:49:49
【问题描述】:
我正在使用具有多维数组作为响应的 Web 服务,当我提醒元素时得到未定义的输出,请参阅下面的代码。
JSON 响应
{
"status":1,
"msg":"cc call history",
"records":{
"1":{
"destination":"Canada - Fixed Others",
"date":"May 05, 2010",
"time_spent":"1 minutes",
"amount_charged":"2.18"
},
"2":{
"destination":"Canada - Fixed Others",
"date":"May 05, 2010",
"time_spent":"1 minutes",
"amount_charged":"2.18"
}
}
}
Javascript 代码
function call()
{
alert('call');
var user_id = sessionStorage.getItem('user_id');
var authentication_key = sessionStorage.getItem('auth_id');
alert(user_id);
alert(authentication_key);
$.ajax({
type: 'GET',
url: 'http://example.com/XXX',
data: {user_id: user_id, authentication_key: authentication_key},
success: function (response)
{
obj = JSON.parse(response);
var status = obj.status;
var msg = obj.msg;
var records;
alert(status);
alert(msg);
alert(records);
if (status === '0')
{
alert(status);
}
else
{
var lnrc = obj.records.length;
alert(lnrc);
for (var i = 0; i < 2; i++)
{
alert('for');
var rc1 = obj.records[i];
alert(rc1);
}
}
},
error: function () {
},
});
}
请提出建议。
【问题讨论】:
-
返回的是什么多维数组,在哪里?您的问题是说您使用的 API 返回 JSON?你能解释一下吗
-
records是一个对象,而不是一个数组.. -
如果在成功回调的顶部使用“console.log(response)”会得到什么?
标签: javascript php arrays web-services multidimensional-array