簡體   English   中英

第1行的JSON解析錯誤,預期為'{','['

[英]JSON parse error on line 1 expecting Expecting '{', '['

我已經嘗試分析幾天了,但仍然不知道如何從使用json_encode編碼的PHP數組中獲取結果。 我是JQuery新手。

這不起作用:

$.post('coordinate_array.php',{},function(data) {  //ERROR HERE EXPECTING SOMETHING??
 results = JSON.parse(data);
 for(i = 0;i < results.length;i++) {
  Paint(results[i].x, results[i].y);
 }
});

我從這個php文件中獲取數據:

<?php
include 'db_conn.php';

header('Content-Type: application/json'); //not sure if i need this here??

$coordinate_sql = "SELECT x_coord, y_coord FROM coordinates";
$result = mysqli_query($conn,$coordinate_sql);

//see if query is good
if($result === false) {
    die(mysqli_error()); 
}

//array that will have number of desks in map area
    while($row = mysqli_fetch_assoc($result)){  

    //get desk array count      
    $desk[] = array('x' => $row['x_coord'], 
                                    'y' => $row['y_coord']);
} //end while loop
    echo json_encode($desk); //encode the array
?>

您沒有echo JSON數據,因此JS調用所請求的頁面將始終為空。 采用:

echo json_encode($desk);

在文件末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM