gpt4 book ai didi

javascript - 无法解析 json 响应,它实际上不为空但返回未定义

转载 作者:行者123 更新时间:2023-12-02 15:59:46 24 4
gpt4 key购买 nike

我有这个 json 代码,我用它来发送 post 请求并检索数据,然后解析它(请参阅下文)。

$.ajax({
url: '/test',
type: 'post',
data: { id : "1"},
dataType: 'json',
success: function(response){
if(response.success){
console.log(JSON.stringify(response.disputeRecord));
$.each(response.disputeRecord, function(index, value){
alert(value.creation_date);

});
}
}
});

这是“response.disputeRecord”的 json 内容(来自控制台的 View )

{"no":7,"employee_id":"MMMFLB003","creation_date":"2015-07-09","log_date":"2015-06-25","log_type":"LOGGED","dispute_time":"07:00","reason":"No Logout data","effect":"Update Logout Data","status":"HR APPROVED","authorized_person":"MMMFLB003","authorized_reason":"You have it","mngmt_updated_at":"2015-07-09 03:12:12","hr_approver":"MMMFLB003","hr_approver_reason":"Approve Dispute Request","hr_updated_at":"2015-07-09 03:13:01"}

如您所见,我试图提醒 json 响应中实际存在的“creation_date”,但它给了我“未定义”,有什么想法、线索吗?

最佳答案

上面的示例显示您没有得到一组 disputeRecord - 只有一个。在这种情况下,each() 会使问题变得困惑,因为没有什么可解析的。试试这个...

$.ajax({
url: '/test',
type: 'post',
data: { id : "1"},
dataType: 'json',
success: function(response){
if(response.success){
console.log(JSON.stringify(response.disputeRecord));
console.log(response.disputeRecord.creation_date);
}
}
});

关于javascript - 无法解析 json 响应,它实际上不为空但返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31317218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com