2016-10-29 62 views
0

我想獲得「名稱」,「團隊」和「職位」等的值,我知道我必須用兩個循環來做嗎?確切地知道jquery - 通過多維數組運行(Json

我的代碼如何......看起來是這樣的:

function getPlayersOfChosenTeam(team, nameOfPlayersTable){ 

$.ajax({ 
    url:'getPlayersOfChosenTeam.php', 
    type:'post', 
    data:{'team':team, 'nameOfPlayersTable':nameOfPlayersTable}, 
    success: function (res) { 

     console.log(res); 


    } 
}); 
} 

和我的資源數據是這樣的:

[ 
{"name":"R. Burnell","team":"Dortmund","position":"GK","points":"4"}, 
{"name":"R. Weidenfeller","team":"Dortmund","position":"GK","points":"45"} 
] 

感謝您的任何提示...

問候

+0

沒有,JSON字符串是在資源內容我張貼基於函數的名稱下面的字符串... – nbg15

+0

你應該看看:如何從異步調用返回響應?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas

+0

thx爲您提供幫助並在我的描述中對我的錯誤感到抱歉... – nbg15

回答

0

其實你有一個數組操作對象,所以你只需要一個循環。

for(var i = 0; i < res.length; i++) { 
    console.log('name: ', res[i].name); 
    console.log('team: ', res[i].team); 
    console.log('position: ', res[i].position); 
    console.log('points: ', res[i].points); 
} 
0

循環通過你的數據,你可以使用jquerys。每()函數是這樣的:

$.ajax({ 
    url:'getPlayersOfChosenTeam.php', 
    type:'post', 
    data:{'team':team, 'nameOfPlayersTable':nameOfPlayersTable}, 
    success: function (res) { 

      $(res).each(function(index, item){ 
      console.log(item.name); 
      console.log(item.team); 
      console.log(item.points); 
      }); 

    } 
0

這是解決方案! ;) 用JSON.parse()來

$.each(JSON.parse(res), function(idx, obj) {  
    alert(obj.name); 
});