0

I followed this Lynda tutorial to get a working Ionic App, and I also followed this Lynda tutorial to set up a MySQL backend with some PHP scripts (I highly recommend both tutorials, by the way!). Now I am trying to get my Ionic app connected to my MySQL database.

The Ionic app currently grabs data from js/data.json, which is just JSON data

In app.js:

.controller('ListController', ['$scope', '$http', '$state',
    function($scope, $http, $state) {
    $http.get('js/data.json').success(function(data) {
      $scope.artists = data.artists;
    });
}]);

I wrote a PHP script that displays the exact same thing as data.json. I thought this would be a good first step towards bridging the gap between the two tutorials.

in Load_Data.php:

 $response = "";
 //omitting code where I fill $response with all 
 //the same info as data.json

 $json = json_encode($response);
 printf("%s", $json);

I have compared the outputs of data.json and Load_Data.php and they are indeed identical. I have also used jsonlint to make sure that it is valid json. However, replacing js/data.json with js/Load_Data.php just makes all the data disappear with no console or server errors.

Also, when I try to go directly to js/Load_Data.php from the url, I immediately get redirected back to index.html, and the browser downloads the file instead of running it. I have no idea why this happens, because I have no code in that file other than encoding and printing the json data.

How can I fix my code so my Ionic app can access my MySQL database?

Kristina
  • 125
  • 1
  • 7
  • Maybe you have to [set the Content-type header to application/json](http://stackoverflow.com/a/4064468/3585500). – ourmandave Apr 22 '16 at 02:47

1 Answers1

0

Can you check with this:

data = JSON.parse(data);    
$scope.artists = data.artists;

It is parsing the data before using it as a JSON object.

Avijit
  • 1,253
  • 13
  • 21