第1行的JSON解析错误期待期待' {',' ['

时间:2014-10-01 14:08:22

标签: javascript php jquery mysql json

我已经尝试解析我的数据几天了,仍然不知道如何从使用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
?>

1 个答案:

答案 0 :(得分:2)

您不是echo您的JSON数据,因此JS调用请求的页面将始终为空。 使用:

echo json_encode($desk);

在您的文件末尾。

相关问题