gpt4 book ai didi

php - 如何在 AngularJS 应用程序中使用 PHP/Mysql 生成的 json

转载 作者:行者123 更新时间:2023-11-29 05:24:48 25 4
gpt4 key购买 nike

我使用 PHP 从 Mysql 数据库生成了一些 Json 数据,如下所示:

equipments.php

<?php

require("config.inc.php");

//initial query
$query = "Select * FROM equipment";

//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Equipment Available!";
$response["equipments"] = array();

foreach ($rows as $row) {
$post = array();
$post["EquipmentId"] = $row["EquipmentId"];
$post["Name"] = $row["Name"];
$post["Ip"] = $row["Ip"];
$post["Brand"] = $row["Brand"];
$post["Location"] = $row["Location"];

//update our repsonse JSON data
array_push($response["equipments"], $post);
}


// echoing JSON response
echo json_encode($response);

} else {
$response["success"] = 0;
$response["message"] = "No Equipment Available!";
die(json_encode($response));
}

?>

这返回了以下数据:

//localhost/equip/equipments.php(在我本地的 apache 服务器上)

{
success: 1,
message: "Equipment Available!",
equipments: [
{
EquipmentId: "1",
Name: "UCH-NET",
Ip: "172.16.32.4",
Brand: "Engenius",
Location: "Top of ITD"
},

{
EquipmentId: "2",
Name: "UCH-PHOUSE",
Ip: "172.16.32.5",
Brand: "Mikrotik",
Location: "Top of ITD"
},

{
EquipmentId: "3",
Name: "UCH-SON",
Ip: "172.16.32.9",
Brand: "MIkrotik",
Location: "SON"
},

{
EquipmentId: "4",
Name: "UCH-GERIATRIC",
Ip: "172.16.32.10",
Brand: "Mikrotik",
Location: "Geriatric"
}
]
}

但是当我尝试在我的 AngularJS 应用程序中像这样使用返回的 Json 时,网页中没有返回任何数据

services.js

'use strict';

var equipServices = angular.module('equipServices', ['ngResource']);

equipServices.factory('Equip', ['$resource',
function($resource){
return $resource( '/equip/equipments.php/');

}]);

equipment.js

    function EquipmentsCtrl ($scope, Equip) {
$scope.setActive('equipments');

$scope.sidebarURL = 'partials/equipment.html';
$scope.currentEquipment = null;

$scope.setEquipment = function (EquipmentId) {
$scope.currentEquipment = $scope.equipments[EquipmentId];
};

$scope.equipments = Equip.query();

}


}

index.html

    <title>IP Library</title>
<script type="text/javascript" src="js/lib/angular.min.js"></script>
<script type="text/javascript" src="js/lib/angular-resource.min.js"></script>
<script type="text/javascript" src="js/controllers/app.js"></script>
<script type="text/javascript" src="js/controllers/equipments.js"></script>
<script type="text/javascript" src="js/controllers/admins.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/services.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.min.css">
</head>
<body>
<div class="container" ng-controller="AppCtrl" >
<h1>IP Library</h1>
<ul class="nav nav-pills">
<li ng-class="equipmentsActive">
<a href="#equipments" >Equipments</a>
</li>
<li ng-class="adminsActive">
<a href="#/admins">Administrators</a>
</li>
</ul>

<div ng-view></div>
</div>

</body>
</html>

我在 services.js 中调用 php 生成的 Json 的方式有问题吗?谢谢

最佳答案

使用 Equip.get() 而不是 Equip.query() 因为您从服务器获取的是对象,而不是数组:

Equip.get(function(result){
if(result.success === 1) // isn't better to return boolean?
$scope.equipments = result.equipments;
// else handle error
});

关于php - 如何在 AngularJS 应用程序中使用 PHP/Mysql 生成的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com