【发布时间】:2016-03-06 14:53:33
【问题描述】:
希望你一切安好,
我想问是否可以使用 $http.get 从同一页面获取数据, 深入细节:
我有一个名为 (MY_FILE.php) 的 php 文件,其中包含:PHP、HTML 和 SCRIPT 代码, 在 PHP 部分我执行了 sql select 语句,我试图在脚本中获取选定的数据以在 html 部分中使用它,这可能吗?!
php部分:
<?php
$connect = mysqli_connect("localhost", "root", "", "database");
$result = mysqli_query($connect, "select * from table");
$data = array();
while ($row = mysqli_fetch_array($result)) {
$data[] = $row;
}
print json_encode($data);
?>
脚本部分:
fetch.controller('dbCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get("MY_FILE.php")
.success(function(data){
$scope.data = data;
})
.error(function() {
$scope.data = "error in fetching data";
});
}]);
当我在 html 部分中使用任何选定的数据时,什么都不会出现并保持空白。
有什么想法吗?!
【问题讨论】:
-
声明 $scope.data = {};在 $http.get 之前的控制器中
-
谢谢,但没有任何改变。
-
您可以注销从请求中返回的数据以确认您确实得到了一些东西吗?设置后退出
$scope.data。 -
使用承诺 docs.angularjs.org/api/ng/service/$http 。您需要使用 .then() 而不是 .success()
-
如果该文件输出
$http请求的 json 以外的任何内容,则该文件将不起作用
标签: php html angularjs controller