将JSON数据解析为SELECT打印UNDEFINED

时间:2013-11-08 06:31:30

标签: javascript jquery ajax json

我从服务器发送的JSON是这样的:

data[
{"userFullName":"Tim, Bill","id":"LOt3","organisation":"FAP","loginSystem":"A","userId":0},{"userFullName":"Bruce, David","id":"LNA","organisation":"ES","loginSystem":"A","userId":0}
] 

在我的AJAX调用成功之后,我按如下方式处理数据:

 success: function (data) {
 javascript: console.log('data' + data);
 $.each(data, function(key, value) {
 javascript: console.log('id' + value.id);
 $('#selectStaff').append('<option value="' + value.id+ '">' + value.userFullName + '</option>');
 });
 }

selectStaff是SELECT控件的ID ...

SELECT控件正在绘制,下拉列表中填充了'undefined'。

The console.log('data' + data)打印以下行:

data[{"userFullName":"Tim, Bill","id":"LOt3","organisation":"FAP","loginSystem":"A","userId":0},{"userFullName":"Bruce, David","id":"LNA","organisation":"ES","loginSystem":"A","userId":0}] 

然而,javascript:console.log('id' + value.id)行打印出'UNDEFINED'

请有人向我描述正确的语法吗?

1 个答案:

答案 0 :(得分:1)

代码val.id

出错

工作正常

Fiddle

脚本

    var user=[{"userFullName":"be, apple","id":"xdsd3","organisation":"FL ","userId":0},{"userFullName":"Mack, David","id":"lol323","organisation":"ES","userId":0}]

$.each(user, function(key, value) {
   console.log('id' + value.id);
                            $('#selectStaff').append('<option value="' + value.id + '">' + value.userFullName + '</option>');
                        });