所以我对我的API有这个ajax GET请求:
$(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,但我需要每个属性
答案 0 :(得分:0)
您访问data.comment
这是未定义的访问权限,如data[0].comment
$('.greeting-content').append(data[0].comment);
您可以使用$.each
获取所有数据
data = [{"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"}];
console.log(data[0].comment);
$.each(data,function(i,v){
console.log(data[i].comment);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 1 :(得分:0)
你正在收集你需要的回合
$.each(JSON.parse(data), function (i, data) {
var row = data; // data.comment
console.log(row);
});
OR
$.each(data, function (i, data) {
var row = data;
console.log(row);
});