在Ajax Json上显示为未定义的数据

时间:2017-11-20 06:32:02

标签: javascript jquery json ajax jsonp

我不得不将json数据解析为html,但得到了未定义的值。

$.ajax({
    type: "GET",
    url: "http://localhost/rest/api/kkb/detail/?key=39E62227E3294114BE8EADF3B6D2F06E&id=4",
    dataType: 'jsonp',
    crossDomain: true,
    success: function(data) {
    for(var i = 0; i < data.length; i++) {
    console.log(data[i].start);
  }
    success: function (response) {
        var trHTML = '';
       $.each(response, function (i, item) {
           trHTML += '<tr><td>' + item.name + '</td><td>' + item.year + '</td><td>' + item.period + '</td></tr>';
       });
       $('#records_table').append(trHTML);
    },

    error: function(e) {
      console.log(e);
    }
  });
};
getUserData();

API网址的结果是:

{"status":true,
 "message":"Detail Kkb",
 "data": { "kkb": { "id":"4",
                    "year":"2014",
                    "periode":"S-I",
                    "name":"TWA Joe Som"
                   }
          }
}

和html代码:<table id="records_table"></table>

但是没有用,它说未定义。当我检查firebug中的日志数据时,加载了数据。非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
var response= [{"status":true,"message":"Detail Kkb","data":{"kkb":{"id":"4","year":"2014","periode":"S-I","name":"TWA Joe Som"}}}];
 var trHTML = '';
       $.each(response, function (i, item) {
           trHTML += '<tr><td>' + item.data.kkb.name + '</td><td>' + item.data.kkb.year + '</td><td>' + item.data.kkb.period + '</td></tr>';
       });
       console.log(trHTML);
       $('#records_table').append(trHTML);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
<tr id="records_table"></tr>
</table>
&#13;
&#13;
&#13;