我的M * EAN应用程序应该使用php从数据库返回数据到Angular端,但是当控制器运行并执行$ http.get时,response.data会返回index.html的源代码。 / p>
我认为帮助我们找到错误所需的所有代码如下。还有一些屏幕截图显示了使用开发人员工具在Chrome中运行应用程序时所发生的事情,以便您确切知道我在说什么。
我们正在运行MAMP服务器,我知道我们的应用程序正在Apache服务器上运行,但我们不知道为什么我们仍然没有获取JSON数据,但HTML是作为JSON响应发送的
以下是随附的代码:
market.php
setof(U, undefeated(U), ListOfUndefeatedPlayers)
marketControllers.js
<?php
header("Access-Control-Allow-Origin: *");
include 'dbConfig.php';
$sel = mysqli_query($con,"select * from Chef");
if (!$sel) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
$data = array();
while ($row = mysqli_fetch_array($sel)) {
$point = array("fullname"=>$row["fullname"],"city"=>$row["city"],"state"=>$row["state"],"zip_code"=>$row["zip_code"],"rating"=>$row["rating"]);
array_push($data, $point);
}
header('Content-type: application/json; charset=utf-8');
echo json_encode($data);
的index.html
angular.module('OrchidApp')
.controller('marketController', function ($scope, $http) {
console.log('ANYTHING');
$scope.users=[];
$http({method: 'GET', url:'market.php', headers:{ 'X-Force-Content-Type': 'application/json' }})
.then(function successCallback(response) {
$scope.users = response.data;
console.log($scope.users);
console.log("There is data here.");
return $scope.users;
}, function errorCallback(response) {
console.log(response.data);
console.log("Error.");
});
});
截图:
答案 0 :(得分:1)
一旦你的后端/ api启动并运行,你可能还想将API的网址添加到$http.get
,该呼叫目前是相对的,因此它会尝试http://localhost:3000/market.php
您可以执行以下操作:
index.html app.js
<script>
window.api = "http://localhost:3001"
</script>
...控制器
$http({method: 'GET', url: window.api + '/market.php'})...
答案 1 :(得分:0)
在这种情况下,可能找不到url /market.php,并且您正在使用的HTTP服务器重定向到index.php。
另一种情况可能是重定向工作正常,但服务器无法识别会话并将您重定向到index.html以进行登录。
尝试在浏览器中打开/market.php查看。
答案 2 :(得分:0)
看起来您正在localhost:3000上运行angular的本地开发Web服务器,它无法处理PHP脚本。您将需要使用WAMP / MAMP,Vagrant或Docker之类的东西来管理您的PHP / mysql环境。