First use 'json_decode' to convert array to JSON response
$test = [ "connections" => [ [ "title" => "Connection 1", "date" => "01-26-2010", "id" => "1" ] ] ];
echo json_encode( $test );
and then process the JSON response in the front-end side.
$.get( 'http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e){
console.log( e.connections );
});
but unfortunately it returns 'undefined'
A view of the response using a JSON editor
I can do this
$.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);
});
});
});
which surely returns the data that I want but I prefer not to do a second loop.
any idea, help please?
Try this. I'm sure you have no header('Content-type:application/json')
in you PHP
if I'm not mistaken. If you don't have, It will return as a string
. You need to parse it by using $.parseJSON
of jQuery
built in method
$.get('http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e) {
$.each($.parseJSON(e),function(i,e){
// code here ...
});
});