Renvoyer un tableau après une requête AJAX
Bonjour,
j'ai un problème avec une fonction qui récupère les données d'un fichier XML et les retourne dans un tableau...
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function liste_participants(){
console.log('Function liste_participants');
var Participants= new Array();
$.ajax({
type: "GET",
url: "data/users.xml",
dataType: ($.browser.msie) ? "text" : "xml",
complete : function(data, status) {
var users = data.responseXML;
$(users).find("user").each(function(){
var id = $(this).attr('id');
var name = $(this).find('name').text();
Participants.push({'id': id,'name': name});
});
}
});
return Participants;
console.log('** Function liste_participants');
} |
Dans ma console Firebug, quand je log le résultat de ma fonction liste_participants, j'obtiens un objet dans le DOM de cette forme, et non pas un tableau...
[Object { id="1", name="nom1"}, Object { id="2", name="nom2"}, Object { id="3", name="nom3"}, 5 more...]
Si dans mon code, je fais:
Code:
1 2 3 4 5
| var part = liste_participants();
for(var i = 0; i < part.length; i++){
console.log(part[i].id);
} |
part.length vaut 0 et je n'arrive pas à accéder a la variable 'id'...
Je vois pas vraiment pas d'où vient le problème, c'est pourquoi je me tourne vers vous..
Merci!