0

I want to get the values of the "name" and the "team" and the "position" etc. and i know i have to do it with two loops right? but i dont exactly know how...

my code looks like this:

function getPlayersOfChosenTeam(team, nameOfPlayersTable){

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

        console.log(res);


    }
});
}

and my res data looks like:

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

Thanks for any hint...

greetings

WEBGONDEL UG
  • 147
  • 1
  • 2
  • 12
  • no, the json string is the the res content i posted the string below... – WEBGONDEL UG Oct 29 '16 at 14:35
  • Based on the name of the function you probably should have a look at: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Oct 29 '16 at 14:38
  • thx for your help and sorry for my mistake at my description... – WEBGONDEL UG Oct 29 '16 at 14:39

3 Answers3

0

Actually you have an array os Objects so you just need one loop.

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);
}
Giovane
  • 1,421
  • 1
  • 15
  • 25
0

To loop trough your data you could use jquerys .each() function like this:

    $.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);
           });

   }
Igor Ilic
  • 1,370
  • 1
  • 11
  • 21
0

THIS IS THE SOLUTION! ;) with Json.parse()

 $.each(JSON.parse(res), function(idx, obj) {       
     alert(obj.name);
 });
WEBGONDEL UG
  • 147
  • 1
  • 2
  • 12