2017-10-17 38 views
0

我有一組是在PHP中生成與json_decode功能JSON數據,這裏是結果:客戶端調用JSON結果顯示爲未定義

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 
    .. 
所示的 keyvalueundefined: undefined

什麼塊

其出錯的腳本?

+1

我看到的不是「關鍵」,並在你的對象「值」屬性。我看到item.pid,item.category,item.title等 – epascarello

回答

0

我想你應該訪問正確的propertiespid,category等反應,

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