代码之家  ›  专栏  ›  技术社区  ›  Ben Koo

客户端调用JSON结果显示为未定义

  •  0
  • Ben Koo  · 技术社区  · 7 年前

    我有一组json数据,这些数据是用php生成的 json_decode 函数,结果如下:

    enter image description here

    然后我创建一个html文档,并尝试使用jquery调用结果 $.getJSON :

    var apiSrc = 'http://localhost/api/ws-data';
    var showData = $('#result');
    
    $(function(){
    
        $.getJSON(apiSrc, function(data) {
            console.log(data);
    
            var items = data.blog.map(function (item) {
                return item.key + ': ' + item.value;
            });
    
            showData.empty();
    
            if(items.length) {
                var content = '<li>' + items.join('</li><li>') + '</li>';
                var list = $('<ul />').html(content);
                showData.append(list);
            }
        });
    
        showData.text('Loading...');
    });
    

    上述结果为:

    REST - Get JSON from PHP file on the server side
    
        undefined: undefined
        undefined: undefined
        undefined: undefined
        undefined: undefined
        ..
    

    它显示了 key value undefined: undefined

    剧本出了什么问题?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sajeetharan    7 年前

    我认为您应该访问正确的 properties 例如 pid,category 响应等,

      var items = data.blog.map(function (item) {
                return item.pid + ': ' + item.category;
     });