【问题标题】:Simple database search engine in AngularJSAngularJS 中的简单数据库搜索引擎
【发布时间】:2016-03-25 08:45:42
【问题描述】:

我正在尝试在 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,知道吗?

【问题讨论】:

  • 确保server.php没有错误,我估计有一个..
  • 我得到一个带有 {{ x.Name }} 的白页。
  • 设置error_reporting(2);并测试..
  • home.php:21 Uncaught SyntaxError: missing ) after argument list 但是我没有看到任何遗漏)?另外,angular.js:4458Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=myApp&amp;p1=Error%3A%2…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.8%2Fangular.min.js%3A19%3A463)
  • 您的 PHP 输出格式错误。将 GET 请求的输出发布到 PHP 脚本?不要将其粘贴到 cmets 中,将其编辑为答案! :-)

标签: javascript php angularjs


【解决方案1】:

这是你的角码

<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);
?>

【讨论】:

  • console.log 返回“未定义”
  • 忽略这个。您在 $scope.names = response.data; 中取得任何成功了吗?是除此之外的错误。这是您要打印的 HTML。 {{ name }}
  • Unexpected token &lt; in JSON at position 0
  • 错误仅来自后端。你的前端在这里没问题。将您的值推送到数组中,然后使用 json_encode() 回显该数组。上面已经给了你工作的 PHP 代码。
【解决方案2】:

好的,我明白了。 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>

server.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);
?>

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

【讨论】:

  • 太棒了。这就是为什么我在那里给了你所有的 PHP 代码。如果您找到解决方案,请尝试接受答案;毕竟努力只为这个。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 2012-04-04
相关资源
最近更新 更多