简体   繁体   English

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

[英]cant parse json response, its actually not empty yet returns undefined

I have this json codes which I used from sending a post request and retrieving data and then parse it (refer below). 我有这个json代码,我用它来发送发布请求并检索数据,然后解析它(请参阅下文)。

$.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);

                });
           }
       }
    });

and this is the json contents of the "response.disputeRecord" (a view from a console) 这是“ response.disputeRecord”(来自控制台的视图)的json内容

{"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"}

as you can see, im trying to alert the "creation_date" which actually exist on the json response but it gives me "undefined", any ideas, clues? 如您所见,即时通讯试图提醒json响应中实际存在的“ creation_date”,但它给了我“未定义”,任何想法,线索?

The example above shows that you're not getting an array of disputeRecord - just one. 上面的示例显示您没有得到一个disputeRecord数组-只是一个。 In that case, the each() is confusing the issue as there is nothing to parse. 在这种情况下,由于没有要解析的内容, each()会使问题感到困惑。 Try this instead... 试试这个...

$.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);
       }
   }
});

You're using $.each on an object, not an array. 您在对象而不是数组上使用$.each The index argument is actually going to be your object's property names and value is going to be the corresponding property value. index参数实际上将是对象的属性名称,而value将是相应的属性值。 You're going to get ("no", 7) , ("employee_id", "MMMFLB003") and so on. 您将得到("no", 7)("employee_id", "MMMFLB003") ,依此类推。

Change to: 改成:

$.each(response.disputeRecord, function(index, value){
    if (index == "creation_date")
        alert(value);
});

index is the property name and value is the property value, so you need to check which property and then alert its value. index是属性名称,value是属性值,因此您需要检查哪个属性,然后提醒其值。

http://jsfiddle.net/44z83jpn/ http://jsfiddle.net/44z83jpn/

or simply access the property directy from the object and remove the $.each: 或直接从对象直接访问属性并删除$ .each:

if(response.success){
    alert(response.disputeRecord.creation_date)
}    

http://jsfiddle.net/j0vL9f05/ http://jsfiddle.net/j0vL9f05/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM