I have the following ajax request:
$.ajax({
type:'POST',
url:'../php/anmelden_info.php',
data:{
kat : 'Freizeitfussballer'
},
success: function(data){
alert(data[0].anmelde_info);
}
});
which alerts undefined
but i don't know why, because when I go the my console and look for the answer I get:
[{"kategorien_id":"3","kat_name":"Fasnacht","anmelde_info":"\n<b>Informationen:.... ......<\/b>\n<br \/>\nDer Turniereinsatz betr\u00e4gt 100.- pro Mannschaft."}]
and I have no idea what I'm doing wrong!
I have the following ajax request:
$.ajax({
type:'POST',
url:'../php/anmelden_info.php',
data:{
kat : 'Freizeitfussballer'
},
success: function(data){
alert(data[0].anmelde_info);
}
});
which alerts undefined
but i don't know why, because when I go the my console and look for the answer I get:
[{"kategorien_id":"3","kat_name":"Fasnacht","anmelde_info":"\n<b>Informationen:.... ......<\/b>\n<br \/>\nDer Turniereinsatz betr\u00e4gt 100.- pro Mannschaft."}]
and I have no idea what I'm doing wrong!
Share Improve this question edited Mar 28, 2013 at 8:26 Beetroot-Beetroot 18.1k3 gold badges39 silver badges44 bronze badges asked Mar 28, 2013 at 8:20 fsulserfsulser 9042 gold badges11 silver badges36 bronze badges 2- Try to console.log(data) and use console to debug and see what exactly e in data object. – Pouki Commented Mar 28, 2013 at 8:25
-
1
Before you try and access the data, do
data = JSON.parse(data)
and see if that helps. – adeneo Commented Mar 28, 2013 at 8:28
3 Answers
Reset to default 3set dataType: json
in you jquery call
As follows
$.ajax({
type:'POST',
url:'../php/anmelden_info.php',
data:{ kat : 'Freizeitfussballer'},
dataType: json,
success: function(data){
console.dir(data);
}
});
or if it not worked try this
$.ajax({
type:'POST',
url:'../php/anmelden_info.php',
data:{ kat : 'Freizeitfussballer'},
dataType: json,
success: function(data){
data = JSON.parse(data);
console.dir(data);
}
});
Try the below code first whilst you have the developer tools open in either Firebug or Chrome. This will show you the structure of the data obejct passed into the sucess method.
$.ajax({
type:'POST',
url:'../php/anmelden_info.php',
data:{
kat : 'Freizeitfussballer'
},
success: function(data){
console.dir(data);
}
});
You have to use $.each()
in your success function:
success: function(data){
$.each(data, function(i, item){
console.log(data.anmelde_info);
});
}
And i suggest you to use console.log()
for a better debugging.