【发布时间】:2026-02-17 04:50:02
【问题描述】:
我的 M*EAN 应用程序应该使用 php 从数据库向 Angular 端返回数据,但是当控制器运行并执行 $http.get 时,response.data 会返回 index.html 的源代码。
我认为帮助我们找到错误所需的所有代码都在下面。还有一些使用开发者工具在 Chrome 中运行应用程序时发生的情况的屏幕截图,以便您确切知道我在说什么。
我们在 MAMP 服务器上运行,我知道我们的应用程序在 Apache 服务器上运行,但我们不知道为什么我们仍然没有获得 JSON 数据,但 HTML 是作为 JSON 响应发送的.
这是随附的代码:
market.php
<?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);
marketControllers.js
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.");
});
});
index.html
<!doctype html>
<html lang="en" ng-app="OrchidApp" >
<head>
<title>Orchid</title>
<meta charset="utf-8">
<base href="/">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.3/angular-cookies.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.4.2/angular-ui-router.min.js"></script>
<script src="/js/app.js"></script>
<script src="/js/authService.js"></script>
<script src="/js/profileController.js"></script>
<script src="/js/marketControllers.js"></script>
<script src="/js/navController.js"></script>
<script src="/js/loginController.js"></script>
<script src="/js/signupController.js"></script>
</head>
<body class="container">
<div ng-controller="marketController">
<p>Above the table.</p>
<table>
<tr ng-repeat="user in users track by $index">
<td>{{user.fullname}}</td>
<td>{{user.city}}</td>
<td>{{user.state}}</td>
<td>{{user.zip_code}}</td>
<td>{{user.rating}}</td>
</tr>
</table>
<p>Below the table.</p>
</div>
</body>
</html>
截图:
【问题讨论】:
标签: javascript php html angularjs http-get