I am new to ionic1 framework
and working on sidemenu ionic app
. I want to get list of playlist from Mysql Database
.
I have tried this in my controller.js :
.controller('PlaylistsCtrl', function($scope,$http) {
$http.get("#/php/list.php")
.success(function (response) {
$scope.playlists = response.records;
});
console.log($scope.playlists);
})
Php file code :
header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
$json[] =array('title'=>'Reggae','id'=>1);
$json[] =array('title'=>'test','id'=>2);
echo json_encode($json);
It gives undefined.
Please help me where I am going wrong.
Your code works asynchron. That is why you add an anonymous function to retrieve the data.
The console.log
is called right after the request but before the response is there. So $scope.playlists
is empty.
You have to put the console.log
inside the success callback to see the result.