2013-03-28 183 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()一個更好的調試。