javascript - JSON.解析: unexpected non-whitespace character after JSON data at line 1

标签 javascript php json

我正在使用一个非常简单的 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';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$app = \Slim\Slim::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: unexpected non-whitespace character after JSON data at line 1.

我的 JavaScript 或 PHP 是否有问题?如何修复? 我认为我传递的代码或数据没有任何问题......

最佳答案

您不应该在 JSON 对象数组上使用 JSON.parse,请改用类似以下内容的内容:

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

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

关于javascript - JSON.解析: unexpected non-whitespace character after JSON data at line 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30026330/

相关文章:

javascript - angularfirebase2列表查询返回未定义

javascript - 如何在 d3 力有向图中将链接渲染为弯头连接器

javascript - 仅当我的文件是运行文件时,如何让 node.js 执行一些代码?

php - Mysql Datetime字段,使用php只更新日期

javascript - 单击链接时如何对表格升序和降序排序

java - JAX-RS 使用 Map<String, Object> 作为 JSON 请求的一部分

Javascript - 值未传递给函数

php - 将 php var 传递给 javascript

java - 将数据结构从 java 传递到 perl(反之亦然)

python - 类型错误 : 'Response' object has no attribute '__getitem__'