简体   繁体   中英

Error Fetching Data From a PHP Server Running MySQL with Angular JS

I am getting a [$http:baddata] error when my Angular app calls my php file. I'm not sure where the error is triggering exactly, but I believe it's a problem constructing the json object! I've attached all relevant code. Any help would be amazing. Thanks in advance :)

在此处输入图片说明

Angular documentation shows this 在此处输入图片说明

This is the PHP file which connects to my database and attempts to return a JSON formatting string back to the calling file

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

This is the Angular app making the request to the PHP file

<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>

Don't construct the JSON the way you are doing, first collect everything inside an array or an object then do echo json_encode($result);

Something like this:

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

Thank to @Madhav for the help. This is what ended up working for me :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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