我对AngularJS和php有问题。不幸的是我的桌子(见下文)是空的。 但是当我尝试在我的(第一个)angularJS站点中加载数据时,我得不到任何数据。这是我的代码:
<!DOCTYPE html>
<html ng-app="employeesApp">
<head>
....
</head>
<body ng-controller="employeesCtrl">
...
<h2>employees</h2>
<table class="table table-striped table-condensed" >
<th></th>
<th>emp_no</th>
<th>birth_date</th>
<th>first_name</th>
<th>last_name</th>
<th>gender</th>
<th>hire_date</th>
<tr ng-repeat="row in employees">
<td><i class="fa fa-pencil-square-o" aria-hidden="true"></i> <i class="fa fa-trash" aria-hidden="true"></i></td>
<td>{{ row.emp_no}}</td>
<td>{{ row.birth_date}}</td>
<td>{{ row.first_name}}</td>
<td>{{ row.last_name}}</td>
<td>{{ row.gender}}</td>
<td>{{ row.hire_date}}</td>
</tr>
</table>
...
<!-- JS -->
<script type="text/javascript" src="../js/angular.min.js"></script>
<script type="text/javascript" src="../js/angular-sanitize.min.js">
</script>
...
<script>
angular.module("employeesApp", [])
.controller('employeesCtrl', function ($scope, $http) {
$scope.employees = [];
$scope.tempEmployeesData = {};
$scope.getEmployeesRecords = function(){
$http.get('employees.php?get_simple_list_employees', {
params:{
'limit':'1000'
}
}).success(function(response){
$scope.employees = response.records;
});
};
});
</script>
</body>
</html>
我认为这与我如何交出AngularJS中的参数有关,但我找不到任何解决方案 我的PHP代码似乎是正确的,因为我在做
时得到了一些约会...
$testarray = array("limit"=>"2");
$testarray = serialize($testarray);
$testarray = urlencode($testarray);
echo "<li><a target='_blank' href='employees.php?cmd=get_simple_list_employees¶m=$testarray'> TEST get_simple_list_employees LIMIT 2 </a></li>";
...
创建的链接类似于
http://.../employees.php?cmd=get_simple_list_employees¶m=a%3A1%3A{s%3A5%3A"limit"%3Bs%3A1%3A"2"%3B}
,结果是
[{"emp_no":"10001","birth_date":"1953-09-02","first_name":"Georgi","last_name":"Facello","gender":"M","hire_date":"1986-06-26"},
{"emp_no":"10002","birth_date":"1964-06-02","first_name":"Bezalel","last_name":"Simmel","gender":"F","hire_date":"1985-11-21"}]
无论如何这里有一些PHP代码(如果你现在需要更多让我)
<?php
// START return just data from the DB here
// Request Handler starts here
// Process Parameters starts here
$command="";
$parameter="";
$test=FALSE;
if (!empty($_GET) && !empty($_GET["cmd"])) {
$command=$_GET["cmd"];
if (!empty($_GET["param"])){$parameter=$_GET["param"];}
}
if (!empty($_GET["test"])){$test=TRUE;}
//Process Parameters ends here
?>
class RequestHandler {
private $db;
public function __construct() {
$config['db']['host'] = "localhost:3306";
$config['db']['user'] = "root";
$config['db']['password'] = "PASSWDHERE";
$config['db']['database'] = "employees";
$db = new mysqli($config['db']['host'], $config['db']['user'], $config['db']['password'], $config['db']['database']);
/* check connection */
if($db->connect_errno){
printf("Connect failed: %s", mysqli_connect_error());
exit();
}
$db->query("SET NAMES utf8");
$this->db = $db;
}
private function getResultArray($result) {
$results_array = array();
if (!$result) return false;
while ($row = $result->fetch_assoc()) {
$results_array[] = $row;
}
return json_encode($results_array);
}
public function get_simple_list_employees($parameter = array()){
if(array_key_exists("limit",$parameter)){
$limit = $parameter['limit'];
} else {
$limit = 100;
}
$query = $this->db->query("SELECT * FROM employees LIMIT ".$limit.";");
return !empty($query)?$this->getResultArray($query):false;
}
$RH = new RequestHandler();
if ( $command != "") {
if ( $parameter != "") {
$parameter = stripslashes($parameter);
$parameter = unserialize($parameter);
$result = $RH->$command($parameter);
}
else {
$result = $RH->$command();
}
echo $result;
exit;
}
感谢您的帮助 罗布
PS我使用此testDB https://github.com/datacharmer/test_db
好的 - 我自己找到的一个错误 - 控制器的新版本 -
<script>
var app = angular.module("employeesApp", [])
app.controller('employeesCtrl', function ($scope, $http) {
$scope.employees = [];
$scope.tempEmployeesData = {};
$http.get('employees.php?cmd=get_simple_list_employees', {
params:{
'limit':'1000'
}
}).success(function(response){
console.log(response);
console.log(response.status);
$scope.employees = response.records;
});
});
</script>
现在我有一些Date返回 - 我可以在控制台中看到它 - 但仍然没有数据显示......
感谢您的帮助 抢劫
下一个版本 - 现在显示数据 - 但不使用LIMIT - 而是使用默认值100
<script>
var app = angular.module("employeesApp", [])
app.controller('employeesCtrl', function ($scope, $http) {
$scope.employees = [];
$scope.tempEmployeesData = {};
$http.get('employees.php?cmd=get_simple_list_employees', {
params:{
'limit':'10'
}
}).success(function(response){
console.log(response);
console.log(response.status);
$scope.employees = response;
});
});
</script>
任何提示?
确定下一个版本然后(不成功) - 但仍然参数限制:10不起作用
<script>
var app = angular.module("employeesApp", [])
app.controller('employeesCtrl', function ($scope, $http) {
$scope.employees = [];
$scope.tempEmployeesData = {};
$http.get('employees.php?cmd=get_simple_list_employees', {
params:{
'limit':'10'
}
}).**then**(function(response){
console.log(response.data);
console.log(response.status);
**$scope.employees = response.data;**
});
});
</script>
上一个正常工作的版本
URL参数和JS参数arr不同 - 所以我将我的PHP代码更改为:
$command="";
$parameter="";
if (!empty($_GET["paramURL"]) && !empty($_GET["paramJS"])) {
echo "error: dont use both parameters at the same time !! you must use paramJS OR paramURL ";
exit; }
if (!empty($_GET) && !empty($_GET["cmd"])) {
$command=$_GET["cmd"];
if (!empty($_GET["paramURL"])){
$parameter=$_GET["paramURL"];
$parameter = stripslashes($parameter);
$parameter = unserialize($parameter);
}
if(!empty($_GET["paramJS"])){
$parameter=$_GET["paramJS"];
$parameter=$parameter[0];
}
}
该功能如下:
public function get_complex_list_employees($parameter = array()){
$sql = "SELECT ";
$sql .= array_key_exists("select",$parameter)?$parameter['select']:'*';
$sql .= " FROM employees";
$sql .= array_key_exists("where",$parameter)?" WHERE ".$parameter['where']:'';
$sql .= array_key_exists("order_by",$parameter)?" ORDER BY ".$parameter['order_by']:'';
$sql .= array_key_exists("limit",$parameter)?" LIMIT ".$parameter['limit']:'';
$query = $this->db->query($sql);
return !empty($query)?$this->getResultArray($query):false;
}
和“电话”
$RH = new RequestHandler();
if ( $command != "") {
if ( $parameter != "") {
$result = $RH->$command($parameter);
}
else {
$result = $RH->$command();
}
echo $result;
exit;
我的AngularJS脚本看起来像
<script>
var app = angular.module("employeesApp", [])
app.controller('employeesCtrl', function ($scope, $http) {
$scope.employees = [];
$scope.tempEmployeesData = {};
$http.get('<?php echo $_SERVER['PHP_SELF'] ?>', {
params:{
cmd: 'get_complex_list_employees',
param: [{limit: 10, select: "*", where: "first_name like \"a%\""}]
},
**paramSerializer: '$httpParamSerializerJQLike'**
}).then(function(response){
//console.log(response.data);
//console.log(response.status);
$scope.employees = response.data;
});
});
</script>
重要步骤
paramSerializer:'$ httpParamSerializerJQLike'
我在这里找到了解决方案 AngularJS GET ajax call with Array parameters
答案 0 :(得分:1)
您的http请求中没有名为cmd
的参数,您可能需要
$scope.getEmployeesRecords = function(){
$http.get('employees.php', {
params:{
'cmd':'get_simple_list_employees',
'limit':'1000'
}
}).then(function(response){
$scope.employees = response.data.records;
});
};