【发布时间】:2016-04-22 12:05:07
【问题描述】:
我关注this Lynda tutorial 获得了一个可以运行的 Ionic 应用程序,我还关注了this Lynda tutorial 使用一些 PHP 脚本设置了 MySQL 后端(顺便说一下,我强烈推荐这两个教程!)。现在我正在尝试将我的 Ionic 应用程序连接到我的 MySQL 数据库。
Ionic 应用目前从 js/data.json 获取数据,这只是 JSON 数据
在 app.js:
.controller('ListController', ['$scope', '$http', '$state',
function($scope, $http, $state) {
$http.get('js/data.json').success(function(data) {
$scope.artists = data.artists;
});
}]);
我编写了一个 PHP 脚本,它显示的内容与data.json 完全相同。我认为这将是弥合两个教程之间差距的良好第一步。
在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);
我比较了data.json 和Load_Data.php 的输出,它们确实是相同的。我还使用了jsonlint 来确保它是有效的 json。但是,将 js/data.json 替换为 js/Load_Data.php 只会使所有数据消失,而不会出现控制台或服务器错误。
另外,当我尝试从 url 直接转到 js/Load_Data.php 时,我会立即被重定向回 index.html,并且浏览器会下载文件而不是运行它。我不知道为什么会发生这种情况,因为除了编码和打印 json 数据之外,我在该文件中有 no 代码。
如何修复我的代码,以便我的 Ionic 应用可以访问我的 MySQL 数据库?
【问题讨论】:
标签: php mysql angularjs ionic-framework