JSON.PARSE:JSON数据在第1行的JSON数据之后出乎意料的非Whitespace字符



我正在使用一个非常简单的javaScript(Angularjs,但这与不相关),例如:

app = angular.module('app', []);
app.controller("NavCtrl",function($scope,$http){
    var serviceBase = 'api/';
    $http.get(serviceBase + 'orders').then(function (results) {
        $scope.categories = results.data;
    var total = 0;
    for(var i = 0; i < $scope.categories.length; i++){
        var categories = $scope.categories[i];   
    }
    });
});

html

<div class="col-md-3" ng-controller="NavCtrl">
      <div class="sm-st clearfix">
      <!--<span class="sm-st-icon st-red"><i class="fa fa-check-square-o"></i></span>-->
       <div class="sm-st-info">
       <i class="fa fa-square"></i>
       <span>Sander</span>
       <p>1 minutes</p>
          <ul>
            <li ng-repeat= "p in categories">
                  {{p.orderID}}
            </li>
          </ul>
      </div>
   </div>
</div>   

api

//PHPSLIM
require '.././libs/Slim/Slim.php';
SlimSlim::registerAutoloader();
$app = new SlimSlim();
$app = SlimSlim::getInstance();
//GET ALL ORDERS
$app->get('/orders', function(){
$extranames = "";
$extrasprice = 0;
    global $conn, $servername, $username, $password, $dbname;
    $sql = "SELECT `orderID`, `customerID`, `storeID`, `orderDate`, `orderDeliveryDate`, `orderStatus` FROM `order`";
    mysql_connect($servername, $username, $password);
    mysql_select_db($dbname);
    $query = mysql_query("SELECT `orderID`, `customerID`, `orderDate`, `orderDeliveryDate`, `orderStatus` FROM `order`");
    $ordersall = array();
    while($line = mysql_fetch_array($query, MYSQL_ASSOC)){
        $orders[] = $line;
    }
    $count = 0;
    $extrasprice = 0;
    foreach($orders as $key => $looporders){
        $extracounter = 1;
        $sql = "SELECT `orderdetailID`, `productID`, `orderdetailAantal`  FROM `orderdetail` WHERE `orderID` = '$looporders[orderID]'";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
            $sql2 = "SELECT `productTitle`, `productPrijs`  FROM `products` WHERE `productID` = '$row[productID]'";
            $result2 = $conn->query($sql2);
                if($result2->num_rows > 0) {
                    while($row2 = $result2->fetch_assoc()){
                        $productTitle = $row2["productTitle"];
                        $productPrijs = $row2["productPrijs"];
                    }
                } 
            $sql3 = "SELECT `customerName` FROM `customers` WHERE `customerID` = '$looporders[customerID]'";
            $result3 = $conn->query($sql3);
                if($result3->num_rows > 0) {
                    while($row3 = $result3->fetch_assoc()){
                        $customerName = $row3["customerName"];
                    }
                }    
            $sql4 = "SELECT `extraID` FROM `orderdetailextra` WHERE `orderdetailID` = '$row[orderdetailID]'";
            $result4 = $conn->query($sql4);
            if ($result4->num_rows > 0) {
                while($row4 = $result4->fetch_assoc()) {
                    $sql5 = "SELECT `extraName`, `extraPrice` FROM `extra` WHERE `extraID` = '$row4[extraID]'";
                    $result5 = $conn->query($sql5);
                    if ($result5->num_rows > 0) {       
                     while($row5 = $result5->fetch_assoc()) {   
                        $extranames = $extranames . $row5["extraName"]. ', ';
                        $extrasprice += $row5["extraPrice"];    
                     }
                    }  
                 }
                }  
                $ordersall[$count] = array(  
                "orderID" => $looporders["orderID"],
                "customerID" => $looporders["customerID"],
                "customerName" => $customerName,
                "orderDate" => $looporders["orderDate"],
                "orderDeliveryDate" => $looporders["orderDeliveryDate"],
                "orderStatus" => $looporders["orderStatus"],
                "orderdetailID" => $row["orderdetailID"],
                "productTitle" => $productTitle,
                "productPrijs" => $productPrijs,
                "aantal" => $row["orderdetailAantal"],
                "extras" => substr($extranames, 0, -2),                
                "extrasPrice" => $extrasprice            
            );                
                $count++;
                $extranames ="";
                $extrasprice = 0;
            }
        }

    }
    echoResponse(200, $ordersall);
});
$app->run();
function echoResponse($status_code, $response) {
    global $app;
    $app->status($status_code);
    header('Content-Type: application/json');
    $app->contentType('application/json');
    echo json_encode($response,JSON_NUMERIC_CHECK);
}

JSON

[{"orderID":4,"customerID":1,"customerName":"Sander","orderDate":"2015-04-20 09:49:06","orderDeliveryDate":"2015-04-20","orderStatus":"done","orderdetailID":2,"productTitle":"Sexy Teacher","productPrijs":4,"aantal":1,"extras":"","extrasprice":""},
{"orderID":5,"customerID":2,"customerName":"Jelle","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":3,"productTitle":"The Coach","productPrijs":3,"aantal":1,"extras":"","extrasprice":""},
    {"orderID":5,"customerID":2,"customerName":"Jelle","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":4,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":"","extrasprice":""},
    {"orderID":5,"customerID":2,"customerName":"Jelle","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":5,"productTitle":"Sexy Teacher","productPrijs":4,"aantal":3,"extras":"","extrasprice":""},
    {"orderID":22,"customerID":11,"customerName":"Tom","orderDate":"2015-04-20 14:15:12","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":22,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":"","extrasprice":""},
    {"orderID":22,"customerID":11,"customerName":"Tom","orderDate":"2015-04-20 14:15:12","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":23,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":"","extrasprice":""}]

现在我遇到了一个错误:

JSON.PARSE:JSON DATA在第1行的JSON之后出乎意料的非Whitespace字符。

我的JavaScript或PHP有问题吗?我认为我传递的代码或数据没有任何问题...

您不应该在JSON objects的数组上使用JSON.parse,请使用以下内容:

for (var i = 0; i < jsonData.length; i++) {
    console.log(jsonData.orderID);
}

另外,我在您的JSON结果中看不到任何字段categories