首先使用'json_decode'将数组转换为JSON响应
$test = [ "connections" => [ [ "title" => "Connection 1", "date" => "01-26-2010", "id" => "1" ] ] ];
echo json_encode( $test );
然后在前端处理JSON响应。
$.get( 'http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e){
console.log( e.connections );
});
但不幸的是它返回'undefined'
使用JSON编辑器的响应视图
我可以做这个
$.get( 'http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e){
$.each(JSON.parse(e),function(i,e){
$.each(e,function(i,e){
console.log(e.title);
});
});
});
肯定会返回我想要的数据,但我不想做第二次循环。
任何想法,请帮忙吗?
试试这个。如果我没弄错的话,我相信你在header('Content-type:application/json')
没有PHP
。如果你没有,它将作为string
返回。你需要使用在$.parseJSON
内置的jQuery
method
来解析它
$.get('http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e) {
$.each($.parseJSON(e),function(i,e){
// code here ...
});
});