【问题标题】:AngularJS get data from MySQL via a php fileAngularJS 通过 php 文件从 MySQL 获取数据
【发布时间】:2015-11-07 16:55:08
【问题描述】:

这是我的问题:

我做了一个非常基本的 angularJS 应用程序,它应该从 php 文件中获取数据并将它们绑定到范围内。所以php文件(链接到MySQL DB)输出是:

[{"hum":"55.36","temp":"20.86","unixtime":"2015-10-29 17:39:42","coreid":"xxxxxx","moist":"0.02","volt":"4.30","soc":"114"},
{"hum":"55.33","temp":"20.84","unixtime":"2015-10-29 17:39:50","coreid":"xxxxxx","moist":"0.00","volt":"4.30","soc":"114"},
{"hum":"55.42","temp":"20.84","unixtime":"2015-10-29 17:39:58","coreid":"xxxxxx","moist":"0.02","volt":"4.30","soc":"114"}]

应用的代码是:

var dashb = angular.module('dashboard', []);

//avoid cross domain issue
dashb.config(['$httpProvider', function($httpProvider) {
$http.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);


dashb.controller('dbCtrl', ['$scope', '$http', function ($scope, $http) {
         $http.get("http://www.domain.com/php_file_url.php")
            .success(function(data) {
                $scope.data = data;
                $scope.hum = data.hum;

            })
            .error(function() {
                $scope.data = "error in fetching data";

            });


    }]);

但是当我运行它时它不起作用。我没有数据。我做错了什么?谢谢

【问题讨论】:

  • 检查浏览器开发工具网络中的实际请求以获取线索。你对it doesn't work 的解释并没有告诉我们什么
  • 在网络标签中我什么也看不到,字面意思:(
  • 这真的没有意义。如果没有一些调试信息,任何人都无法为您提供帮助
  • 我能得到的唯一调试信息是控制台中的这个错误:Error: $injector:modulerr Module Error
  • 这很重要。您的应用程序因此没有初始化。需要弄清楚它指的是哪个模块并阅读完整的错误

标签: php mysql angularjs http


【解决方案1】:

如果您确定从查询中获取数据,则只需将其作为 JSON 发送。在echo $res; 行之前添加:

header('Content-Type: application/json');

另外,在您的代码中,更改以下内容

$http.defaults.useXDomain = true;

$httpProvider.defaults.useXDomain = true;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多