所以我有這個AJAX GET請求我的API:無法從Ajax響應獲取數據
$(document).ready(function() {
//id=$("#id").val();
url="api.php/fcomment/"+5;
$.ajax({
type: "GET",
url: url,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data)
{
console.log(data);
alert(data);
$('.greeting-content').append(data.comment);
}
});
});
我得到的JSON數據,它的結果是:
[{"id":"5","comment":"Test","post_date":"18:17 18.05.2017","forum":"2","user":"0"},{"id":"8","comment":"Test2","post_date":"18:05 24.05.2017","forum":"2","user":"7"}]
我會喜歡從JSON格式中獲取值,但沒有添加到div。如果我添加JSON.stringify圍繞數據然後我得到整個JSON,但我本身需要每個屬性
使用JSON.parse(數據); – JYoThI
$ .each方法,遍歷數據 –