【问题标题】:JSON.parse() not working with variable type string but it works with its content if used directlyJSON.parse() 不适用于可变类型字符串,但如果直接使用,它可以处理其内容
【发布时间】:2019-05-20 02:14:46
【问题描述】:

我使用一个简单的代码从一个 php 页面获取一个字符串,我曾多次使用该代码从 php 文件中获取字符串响应。这是我第一次尝试将此结果转换为 json 对象(或数组)。

当我在响应中使用 JSON.parse 时,我收到如下所示的错误。如果我作为 JSON.parse() 的参数传递我已经控制台日志的文本(即从控制台复制,因为它被传递给 javascript)它完美地工作。


    xmlhttp=new XMLHttpRequest();
  } else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
        var result = this.responseText;

        resultsObj = JSON.parse(result);
  }

php代码如下:

<?php
if (!session_id()) {
    @session_start();
}
header('Content-Type: text/json');
require 'controlleur/connexionDB.php';
$sql = "SELECT * FROM depart";

$result = $conn->query($sql);
$liste .= "[";
$compteur = 0;

while ($row = $result->fetch()) {
    if ($compteur != 0) {
        $liste .= ", ";
    }
    $liste .= "{ \"idDepart\" : \"$row->idDepart\", \"idCircuit\" : \"$row->idCircuit\", \"dateDebut\" : \"$row->dateDebut\", \"nbPlaces\" : \"$row->nbPlaces\", \"prix\" : \"$row->prix\", \"titrePromotion\" : \"$row->titrePromotion\", \"rabais\" : \"$row->rabais\" }";
    $compteur++;
}
$liste .= "]";
$reponse = $liste;

echo $reponse;

我收到此错误: SyntaxError:JSON.parse:第 1 行第 1 列出现意外字符

变量“result”在控制台中的结果是这样的:

'[
{ "idDepart" : "1", "idCircuit" : "5", "dateDebut" : "2019-06-02", "nbPlaces" : "30", "prix" : "4000", "titrePromotion" : "vfv", "rabais" : "10" }, 
{ "idDepart" : "2", "idCircuit" : "5", "dateDebut" : "2019-06-10", "nbPlaces" : "30", "prix" : "6000", "titrePromotion" : "ded", "rabais" : "4" }, 
{ "idDepart" : "3", "idCircuit" : "5", "dateDebut" : "2019-07-02", "nbPlaces" : "30", "prix" : "7000", "titrePromotion" : "ded", "rabais" : "6" }
]'

【问题讨论】:

  • 第 1 行第 1 列是什么字符?
  • 就是这样
  • 你能展示你的返回json的服务器端代码吗??
  • 这个字符是撇号吗?
  • 您可以在 php 中构建一个包含您想要的所有项目的数组,然后在其上使用 json_enocde,或者一次构建一个项目并在其上使用 json_enocde,如果您将其包含在 "[ "和"]"。

标签: javascript php ajax


【解决方案1】:

您不应尝试通过操作字符串来创建 json,而应使用内部 json_encode 方法。

$result = $conn->query($sql);
$response = [];
while ($row = $result->fetch()) {
    $response[] = [
        'idDepart' => $row->idDepart,
        'idCircuit' => $row->idCircuit,
        'dateDebut' => $row->dateDebut,
        'nbPlaces' => $row->nbPlaces,
        'prix' => $row->prix,
        'titrePromotion' => $row->titrePromotion,
        'rabais' => $row->rabais
    ];
}

echo json_encode($response);

像这样,你100%肯定有:

  • 如果您的输入无法编码,则出现 PHP 错误。
  • 显示有效的 json。

--- 编辑 ---

我看到你用法语编码,注意特殊字符(如 é à ...)。你应该到处都有 UTF-8 编码。

  • 你的 php.ini
  • 您的 PDO 连接
  • 您的数据库字符集、表和字段。

    $dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf-8", $dbUser, $dbPass); $dbHandle->exec("SET CHARACTER SET utf8");

【讨论】:

  • 效果很好,感谢您向我展示如何做到这一点。您应该在每一行之后添加“,”。
  • 是的,那是真的 :) 请检查我的编辑,这可能对你有帮助
  • 非常感谢,我会保存您的答案以供将来参考
【解决方案2】:

您可以尝试将 php 脚本简化为此吗?

<?php
if (!session_id()) {
    @session_start();
}

header('Content-Type: application/json');
require 'controlleur/connexionDB.php';
$sql = "SELECT * FROM depart";

$result = $conn->query($sql);

$liste = array();
while($row = $result->fetch()){
  $list[] = [
    "idDepart" => $row->idDepart,
    "idCircuit" => $row->idCircuit,
    "dateDebut" => $row->dateDebut, 
    "nbPlaces" => $row->nbPlaces, 
    "prix" => $row->prix, 
    "titrePromotion" => $row->titrePromotion, 
    "rabais" => $row->rabais
  ];
}

echo json_encode($liste);

我使用 json_encode() 而不是手动编写字符串,我认为这样可以更好地避免拼写错误。

【讨论】:

  • 是的,基本上就像 Yanis-git 回答的那样
【解决方案3】:

确保您在 php 脚本中正确设置了标头

<?php header('Content-Type: text/json');

【讨论】:

  • 它是application/json,如果代码(包括问题中的代码)不注意标题,则不会有任何影响。
猜你喜欢
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多