Javascript 第1行出现JSON分析错误,应为';{';,';[';

Javascript 第1行出现JSON分析错误,应为';{';,';[';,javascript,php,jquery,mysql,json,Javascript,Php,Jquery,Mysql,Json,几天来,我一直在尝试解析数据,但仍然不知道如何从使用json_encode编码的PHP数组中获取结果 这不起作用: $.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);

几天来,我一直在尝试解析数据,但仍然不知道如何从使用json_encode编码的PHP数组中获取结果

这不起作用:

$.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);
 }
});
$.post('coordinate_array.php',{},函数(数据){//此处出现错误,需要一些信息??
results=JSON.parse(数据);
对于(i=0;i
我从这个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
?>

您没有
回显
JSON数据,因此JS调用请求的页面将始终为空。 使用:


在文件的末尾。

您没有显示任何内容,您需要执行:
print(json_encode($desk));
。此外,
die(mysqli_error())
不是json,因此如果出现问题,它将是无效的json……您的PHP文件中可能还有一个错误,导致一些通知(或其他任何情况)在JSON之前打印。您的错误是?我能看到的唯一一件事是您没有输出JSON数据。
JSON_encode()
返回编码字符串。它不进行输出,因此您的脚本实际上是无用的,因为没有输出到客户端。您需要
echo JSON_encode($desk)
我添加了回音,但屏幕上仍然没有绘制任何内容。{x:“20”,“y:“20”},{“x:“30”,“y:“30”},{“x:“40”,“y:“40”},{“x:“50”,“y:“50”}]…我在JQuery中会做错什么?@MarcB这是我的错误:第1行的解析错误:$.post('coordination"a ^期望'{','['为什么要执行JSON.parse,返回的数据是一个数组,而不是要分析的字符串。@mario此请求的结果是什么?如果不是浏览器中的“网络”选项卡,您应该使用
curl
之类的工具对其进行调试。
echo json_encode($desk);