使用jQuery和PHP我得到了json数组,但不知何故我无法从中获取数据。任何人都可以解释为什么会这样吗?
$.ajax({
type: "POST",
url: "php/global_functions.php",
data: {callFunction: "getNewsTitles"},
cache: false,
success: function(result){
alert(result);
//[{"heading":"Ritens pie sienas tiek nozagts","text":"Zilu 2008.gada BMW m\u0113s at\u013cauties tom\u0113r nevaram"},{"heading":"Dzied\u0101t\u0101ja Aliwka par CityTaxi","text":"Teksts 123 Teksts"},{"heading":"Kvalit\u0101tes uzlabo\u0161ana P\u0101rdaugav\u0101","text":"Fant\u0101zija nozaga kaimi\u0146a ka\u0137i"},{"heading":"CityTaxi papla\u0161ina autoparku","text":"Fant\u0101zija sit augstu vilni"},{"heading":"CityTaxi iekaro R\u012bgu","text":"Te iet kaut k\u0101ds teksts, piem\u0113ram - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris pellentesque nisl id lobortis congue. Ut commodo tortor eget dapibus gravida. Sed accumsan orci ac ante dignissim feugiat."}]
alert(result[1].heading);
//undefined
}
});
我评论了警报中显示的结果。为什么它会给我undefined
?
答案 0 :(得分:1)
使用以下方法解析结果:
var returnedData = JSON.parse(response);
您还可以将数据类型设置为JSON:
$.ajax({
type: "POST",
dataType: "json",
url: "php/global_functions.php",
data: {callFunction: "getNewsTitles"},
cache: false,
success: function(result){
alert(result);
alert(result[1].heading);
}
});
或者使用$.getJSON jQuery方法。