Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript SyntaxError:JSON.parse:解析PHP输出时出现意外字符_Javascript_Php_Json - Fatal编程技术网

Javascript SyntaxError:JSON.parse:解析PHP输出时出现意外字符

Javascript SyntaxError:JSON.parse:解析PHP输出时出现意外字符,javascript,php,json,Javascript,Php,Json,我的php返回如下内容: if(!$result = $db->query($sql)){ echo $db->error; die('There was an error running the query [' . $db->error . ']'); } echo 'Total results: ' . $result->num_rows . "\n"; while($row = $result->fetch_assoc(

我的php返回如下内容:

  if(!$result = $db->query($sql)){
    echo $db->error;
      die('There was an error running the query [' . $db->error . ']');
  }
  echo 'Total results: ' . $result->num_rows . "\n";

  while($row = $result->fetch_assoc()){
      echo json_encode($row);
      echo "\n";
  }
在我的javascript中,如果是json对象,我希望将输出放在页面上,如果是其他对象,则只需将其记录到控制台:

var divOutput = function(output) {
  try {
    // var x = output.trim();
    // x = JSON.parse(JSON.stringify(x));
    var x = JSON.parse(output);
    console.log(x);
    $("#dbOut").html(JSON.stringify(x, null, '\t'));
  } catch (e) {
    console.log(output);
    console.log(e);
  }
}

var getPlayerByID = function() {
  var myNumber = document.getElementById("PlayerInput").value;
  $.ajax({
    url : "db_funcs.php",
    data : {
      action : 'getPlayerByID',
      a : myNumber,
    },
    type : 'post',
    success : function(output) {
      divOutput(output);
    }
但是,当我查询数据库时,它抛出了JSON解析错误。我该怎么做? }) }这是错误的

while($row = $result->fetch_assoc()){
  echo json_encode($row);
  echo "\n";
}
像这样试试

$arr = array();
while($row = $result->fetch_assoc()){
  $arr[] = ($row);
}

echo json_encode($arr);
只使用
json\u encode()
一次,使用数组在
while()中存储值这是错误的

while($row = $result->fetch_assoc()){
  echo json_encode($row);
  echo "\n";
}
像这样试试

$arr = array();
while($row = $result->fetch_assoc()){
  $arr[] = ($row);
}

echo json_encode($arr);

只使用
json\u encode()
一次,使用数组在
while()
中存储值。它会抛出错误,因为您不仅仅是回显json

排队

echo 'Total results: ' . $result->num_rows . "\n"
您已经回显了一些非json编码的内容。 使用此代码,它应该可以工作:

//echo 'Total results: ' . $result->num_rows . "\n";  <---- this is not json
$arr = [];
while($row = $result->fetch_assoc()){
     $arr[] = $row; //save each row in array
    //echo "\n";
}
echo json_encode($arr); //encode all data as json

它会抛出错误,因为您不是唯一的回显json

排队

echo 'Total results: ' . $result->num_rows . "\n"
您已经回显了一些非json编码的内容。 使用此代码,它应该可以工作:

//echo 'Total results: ' . $result->num_rows . "\n";  <---- this is not json
$arr = [];
while($row = $result->fetch_assoc()){
     $arr[] = $row; //save each row in array
    //echo "\n";
}
echo json_encode($arr); //encode all data as json

JSON必须作为单个单一JSON字符串输出。您正在构建多个单独的json字符串,这是非法语法

将JSON视为相当于javascript变量赋值的右侧:

var foo = this_part_is_json;
e、 你需要有

var foo = [[stuff from row 1], [stuff from row2], etc...];
但我们正在生产

var foo = [stuff from row1][stuff from row2];
                           ^---syntax error would occur here
你需要

$arr = array();
while($row = fetch from db) {
   $arr[] = $row;
}
echo json_encode($arr);

JSON必须作为单个单一JSON字符串输出。您正在构建多个单独的json字符串,这是非法语法

将JSON视为相当于javascript变量赋值的右侧:

var foo = this_part_is_json;
e、 你需要有

var foo = [[stuff from row 1], [stuff from row2], etc...];
但我们正在生产

var foo = [stuff from row1][stuff from row2];
                           ^---syntax error would occur here
你需要

$arr = array();
while($row = fetch from db) {
   $arr[] = $row;
}
echo json_encode($arr);

你在jsonlint.com上测试过JSON了吗。我认为try/catch语句可以处理这个问题。不,它将整个输出视为一件事,因此崩溃。您可以标记给出的一个答案,以便其他用户知道问题是如何解决的。啊,好的。我认为try/catch语句可以处理这个问题。不,它将整个输出视为一件事,因此崩溃。您可以标记给出的一个答案,以便其他用户知道问题是如何解决的。