【问题标题】:Error Fetching Data From a PHP Server Running MySQL with Angular JS使用 Angular JS 从运行 MySQL 的 PHP 服务器获取数据时出错
【发布时间】:2017-09-30 20:41:04
【问题描述】:

当我的 Angular 应用程序调用我的 php 文件时,我收到 [$http:baddata] 错误。 我不确定错误究竟在哪里触发,但我相信这是构造 json 对象的问题!我附上了所有相关的代码。任何帮助都会很棒。在此先感谢:)

Angular 文档显示了这一点

这是连接到我的数据库并尝试将 JSON 格式字符串返回给调用文件的 PHP 文件

<?php 
echo '<script>console.log("Inside php file!")</script>';
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

require_once('mysqli_connect.php');

$query = "SELECT rideid, destination, price, capacity, userid, origin, departDate, vehicle FROM rides";

$result = @mysqli_query($dbc, $query);

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
     if ($outp != "") {$outp .= ",";}
    $outp .= '{"Origin":"'  . $rs["origin"] . '",';
    $outp .= '"Destination":"'   . $rs["destination"]        . '",';
    $outp .= '"Price":"'. $rs["price"]     . '"}';
    $outp .= '"Capacity":"'. $rs["capacity"]     . '"}';
}

$outp ='{"records":['.$outp.']}';
//echo '<script>console.log(JSON.stringify('.$outp.'))</script>';
$conn->close();

echo($outp);

这是向 PHP 文件发出请求的 Angular 应用

<html>
<head>
    <!-- Include Angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
</head>
<body>

<!-- Ride Table -->
     <div ng-app = "rideAppTest" ng-controller= "rideCtrlTest">
        <table>
          <tr ng-repeat = "x in names">
            <td>{{x.Origin}}</td>
            <td>{{x.Destination}}</td>
            <td>{{x.Price}}</td>
            <td>{{x.Capacity}}</td>
          </tr>
        </table>
     </div>
<!-- End Ride Table -->

<!--Ride Table Script-->
<script>
  var app = angular.module('rideAppTest', []);
  app.controller('rideCtrlTest', function($scope, $http) {
    $http.get("angularFilter.php")
    .then(
      function (response) {
        $scope.names = response.data.records;
      },
      function(data){
        console.log("Error!" + data);
      }

        );
});
</script>
</body>
</html>

【问题讨论】:

    标签: javascript php mysql angularjs internal-server-error


    【解决方案1】:

    不要按照你的方式构造 JSON,首先收集数组或对象中的所有内容,然后执行echo json_encode($result);

    类似这样的:

    $outp = [];
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
        $outp[] = $rs;
    }
    echo json_encode(["records" => $outp]);
    

    【讨论】:

    • 没关系,永远不要以这种方式构造 JSON,这甚至不是正确的方式。它只会使代码复杂化并增加 LOC。
    • 是的!另外,您如何将代码格式化为 cmets 中的块?堆栈交换只会让我添加一个单行块?
    • 选择整个代码块然后点击格式化按钮:)
    【解决方案2】:

    感谢@Madhav 的帮助。这就是最终为我工作的东西:)

    while($rs = mysqli_fetch_array($result)) {
            $myArray[] = array(
                'Origin' => $rs["origin"],
                'Destination' => $rs["origin"],
                'Price' => $rs["price"],
                'Capacity' => $rs["capacity"]
    
            );
        }
        echo json_encode(["records" => $myArray]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      相关资源
      最近更新 更多