繁体   English   中英

AngularJS 中的简单数据库搜索引擎

[英]Simple database search engine in AngularJS

我正在尝试在 AngularJS 中制作一个简单的搜索引擎。 我的客户端和服务器之间的通信有问题。 我正在尝试遵循 w3schools 指南http://www.w3schools.com/angular/angular_sql.asp

这是 home.php 主体:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">      
</script> 
</head>

<body>
<div ng-app="myApp" ng-controller="customersCtrl"> 
<table>
    <tr ng-repeat="x in names">
        <td>{{ x.Name }}</td>
    </tr>
</table>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("server.php")
    .then(function (response) {$scope.names = JSON.parse(response.data.records);});

});

</script>

这是 server.php :

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(2);

$conn = new PDO('mysql:host=localhost;dbname=mygaloo;charset=utf8', 'root',    ''); 
$result = $conn->query("SELECT * FROM associations"); 
$outp = "";

while($donnees = $query->fetch()) 
{
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"Name":"'  . $donnees["nom"] . '"}';
}

$outp ='{"records":['.$outp.']}';
$conn->close();


echo($outp);
?>

但是,我开始出现这个错误: angular.js:12520SyntaxError: Unexpected token u in JSON at position 0 ,知道吗?

这是你的角度代码

<script>
        var app = angular.module('myApp', []);
        app.controller('customersCtrl', function($scope, $http) {   
                $http.get("server.php")
                .then(function (response) {
                    $scope.names = response.data;
                }).then(function(response){
                    console.log(response)
                })

        });
    </script>

这是您的 PHP 代码。 希望它会帮助你。

<?php
  header("Access-Control-Allow-Origin: *");
  header("Content-Type: application/json; charset=UTF-8");

  $conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', ''); 
  $result = $conn->query("SELECT * FROM associations"); 
  $outp = [];
  while($rs = $result->fetch(MYSQLI_ASSOC)) {
    if ($outp != "")
    array_push($outp,$rs["nom"]);
  }
 $outp =json_encode($outp);
 $conn = null;
 echo($outp);
?>

好,我知道了。 Arun Shinde 的回答是正确的,但问题来自:

while($rs = $result->fetch(MYSQLI_ASSOC)) {

我正在使用 PDO,所以在这里使用 MYSQLI 显然行不通。 所以这里是完整的代码。 主页.php :

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">     
    </script> 
</head>

<body>
    <div ng-app="myApp" ng-controller="customersCtrl"> 
    <table>
        <tr ng-repeat="x in names"> <td>{{ x }}</td> </tr>
    </table>
    </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {   
            $http.get("server.php")
            .then(function (response) {
                $scope.names = response.data;
            }).then(function(response){
                console.log(response)
            })

    });
    </script>
</body>

服务器.php:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new PDO('mysql:host=localhost;dbname=mygaloo;charset=utf8', 'root', ''); 
$result = $conn->query("SELECT * FROM associations"); 
$outp = [];
while($rs = $result->fetch()) {
    if ($outp != "")
    array_push($outp,$rs["nom"]);
}
$outp =json_encode($outp);
$conn = null;
echo($outp);
?>

非常感谢大家的耐心等待!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM