angularjs 从PHP中获取Angular 数据未处理错误

jk9hmnmh  于 2023-03-07  发布在  Angular
关注(0)|答案(2)|浏览(150)

我得到了一个错误,它没有告诉我任何事情。我有点新的Angular ,所以它可能是一个非常愚蠢的错误,但我不能弄清楚。
它指出存在语法错误:

SyntaxError: Unexpected token /
at Object.parse (native)
at fromJson (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:1252:14)
at defaultHttpResponseTransform (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:9414:16)
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:9505:12
at forEach (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:336:20)
at transformData (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:9504:3)
at transformResponse (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:10276:23)
at processQueue (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:14745:28)
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:14761:27
at Scope.$eval (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js:15989:28)

我调用控制器的html代码:

<div ng-controller="dbTestCtrl">
  <table>
    <tr ng-repeat="x in names">
      <td>{{ x.Name }}</td>
      <td>{{ x.Country }}</td>
    </tr>
  </table>
</div>

我的Angular 控制器:

module.controller('dbTestCtrl', function($scope, $http) {
   $http.get(PHP_API + "datahandler/dbTest.php")
   .then(function (response){
      $scope.names = response.data.records;});
});

和我的php文件,这是工作,据我所见:

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

  $conn = new mysqli("localhost", "root", "", "cmweb");

  $result = $conn->query("SELECT loginName, userID, createDate FROM users");

  $outp = "";
  while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {
        $outp .= ",";
    }
    $outp .= '{"Name":"'  . $rs["loginName"] . '",';
    $outp .= '"ID":"'   . $rs["userID"]        . '",';
    $outp .= '"Created":"'. $rs["createDate"]     . '"}'; 
  }
  $outp ='{"records":['.$outp.']}';
  $conn->close();

  echo($outp);
?>

就像我说的,我真的是一个新的棱角,我只是不能自己弄清楚,所以如果有人能帮助我就太好了:)
谢谢

ef1yzkbh

ef1yzkbh1#

可能是你的json格式不对。尝试使用json_encode来避免自己写那些引号和括号。

$outp = array();

while ($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    $outp[] = array('Name' => $rs["loginName"], 
                    'ID' => $rs["userID"], 
                    'Created' => $rs["createDate"]);
}

echo json_encode(array('records' => $outp));
7vhp5slm

7vhp5slm2#

你的错误在于回报

echo json_encode($outp);

相关问题