I'm having trouble to retrieve the value from a javascript Object returned to me by a ajax call to a php webservice. I confirmed that the JSON returned is correct as you can see below:
{
"json": [{
"campeonato": {
"id_campeonato": "630",
"nome_campeonato": "Copa do Mundo\r",
"nome_pais": "África\r"
}
}, {
"campeonato": {
"id_campeonato": "11",
"nome_campeonato": "Série A\r",
"nome_pais": "Brasil\r"
}
}]
}
I'm trying to get the object using response.json[i].value but whenever I try to alert this I get the message undefined.
And here is the funcition I'm using to the ajax call:
webservice.js
function listaCampeonatos(){
$.ajax({
url: 'http://localhost/projetos/centraljogos/webservice/listagem.php',
type: 'GET',
dataType: 'json',
data: {type:'listaCampeonatos'},
ContentType: 'application/json',
success: function(response){
//alert('Listagem bem sucedida!');
//$('#resultado').html(JSON.stringify(response));
//console.log(response);
alert(JSON.stringify(response));
for (i=0 ; i<response.json.length ; i++){
//alert('Entrou no for / Pos. array: '+i);
console.log(response.json[i].nome_campeonato);
alert(response.json[i].nome_campeonato);
}
},
error: function(err){
alert('Ocorreu um erro ao se comunicar com o servidor! Por favor, entre em contato com o administrador ou tente novamente mais tarde.');
console.log(err);
}
});
}
So, what am I doing wrong? Thaks in advance!
valueproperty in your json object. Try this:response.json[i].campeonato.id_campeonato