Json上显示为未定义

Json上显示为未定义

本文介绍了数据在Ajax Json上显示为未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持将json数据解析为html,但是得到了未定义的值.

I got stuck to parsing the json data to html, but got the undefined value.

$.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网址的结果是:

the result from the API url is:

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

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

但不起作用,它表示未定义.当我检查萤火虫中的日志数据时,数据已加载.非常感谢您的帮助

but is not working, it said undefined. when i check on log data in firebug, the data loaded. Really appreciate for any help

推荐答案

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);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
<tr id="records_table"></tr>
</table>

这篇关于数据在Ajax Json上显示为未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:53