我想使用.each()函数遍历一个JSON对象,但我在语法上遇到了困难。
Ajax调用结果为"undefined":
$('.showGroup a').on('click', function(e) {
e.preventDefault();
var _href = $(this).attr("href");
$.ajax({
dataType: 'json',
url : _href,
type : 'GET',
success : function(response) {
$("#replace").empty();
display = response;
$.each(display, function(i, member) {
alert(display[i].company_name);
});
},
error : function() {
console.log('error');
}
});
});
如果我只是调用alert(display[i]),我的JSON对象看起来像这样:
{"total":134,"per_page":15,"current_page":1,"last_page":9,"from":1,"to":15,"data":[{"id":"89","company_name":"test" ...
我也尝试嵌套另一个 .each() 循环,但是我收到了这个响应:Uncaught TypeError: Cannot use 'in' operator to search for '17381'
我查看了几个SO答案,并尝试了各种样式的 .each 循环,但我总是得到未定义错误。
我错过了什么?
编辑 我正在后端构建类似这样的 JSON:
$members = $this->user->getMemberIds($loggedIn->id, $display, $associationFilter);
$this->arrResponse['members'] = $members->toJson();
return Response::json($this->arrResponse);