2013-03-28 186 views
1

我有以下的Ajax请求:Ajax请求返回undefined

$.ajax({ 
    type:'POST', 
    url:'../php/anmelden_info.php', 
    data:{ 
     kat : 'Freizeitfussballer' 
    }, 
    success: function(data){ 
     alert(data[0].anmelde_info); 
    } 
}); 

它提醒undefined,但我不知道为什么,因为当我走在我的控制台和寻找答案,我得到:

[{"kategorien_id":"3","kat_name":"Fasnacht","anmelde_info":"\n<b>Informationen:.... ......<\/b>\n<br \/>\nDer Turniereinsatz betr\u00e4gt 100.- pro Mannschaft."}] 

我不知道我做错了什么!

+0

尝试CONSOLE.LOG(数据),并使用控制台调试和看什么完全来自数据对象。 – Pouki 2013-03-28 08:25:34

+1

在尝试访问数据之前,请执行'data = JSON.parse(data)'并查看是否有帮助。 – adeneo 2013-03-28 08:28:42

回答

3

设置dataType: json在你的jQuery调用
如下

$.ajax({ 
    type:'POST', 
    url:'../php/anmelden_info.php', 
    data:{ kat : 'Freizeitfussballer'}, 
    dataType: json, 
    success: function(data){ 
     console.dir(data); 
    } 
    }); 

,或者如果它不工作试试这个

$.ajax({ 
    type:'POST', 
    url:'../php/anmelden_info.php', 
    data:{ kat : 'Freizeitfussballer'}, 
    dataType: json, 
    success: function(data){ 
    data = JSON.parse(data); 
    console.dir(data); 
    } 
    }); 
+0

当然''dataType:“json”,' – 2013-03-28 08:39:35

0
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); 
    } 
    }); 
0

您的成功函数中使用$.each()

success: function(data){ 
    $.each(data, function(i, item){ 
     console.log(data.anmelde_info); 
    }); 
} 

我建议你使用console.log()一个更好的调试。