【问题标题】:PHP-mysqli-Database with json: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dataPHP-mysqli-Database with json: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
【发布时间】:2017-06-17 18:28:43
【问题描述】:

我知道已经有对错误消息“SyntaxError:JSON.parse:JSON 数据第 1 行的意外字符”的请求,我已阅读该消息。不幸的是,我仍然无法在我的代码中修复这个错误,尽管我认为已经删除了任何 HTML 代码。你们有人有想法吗?我很感激每一个提示。 请不要惊讶,这个PHP文件还有其他文件,这就是为什么这个数据库中既不显示数据库也不显示数据的原因。

(你好祖萨门, ich weiß, dass es für die Fehlermeldung "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data" schon Anfragen gibt, die ich mir durchgelesen habe。 Leider habe ich diesen Fehler in meinem Code bisher immer noch nicht beheben können, obwohl ich dachte, jeglichen HTML-Code entfernt zu haben。 Hat jemand von euch vielleicht eine Idee, oder findet gar den Fehler? Bitte nicht wundern, zu dieser PHP Datei gehören noch weitere Dateien, weshalb hier in dieser Datenbank "students" weder Daten eingetragen noch in einer Tabelle angezeigt werden。)

代码:

    <?php
//Fehlermeldung: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
header('Content-Type: application/json');

$host = 'localhost';
$user = 'root';
$password = '';
$database = 'mmn1617';

$c = mysqli_connect($host, $user, $password, $database);

/*
 * here's a query that creates a table "students" if it does not exist already.
 * The table contains a PersonID, the primary key which will be automatically incremented
 */
$createstudentsQuery = "CREATE TABLE IF NOT EXISTS students(
  ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  firstName VARCHAR(255) NOT NULL,
  lastName VARCHAR(255) NOT NULL,
  semester VARCHAR(255) NOT NULL,
  course VARCHAR(255) NOT NULL,
  grade DOUBLE NOT NULL
)";

//we execute the query here, the result is returned to the $result
$result = mysqli_query($c, $createstudentsQuery);

/*result will be "true" if the query was successful
Note: The query doesn't contain the PersonID, because
it will be automatically created by MySQL*/ 

if (!$result) {
    //the $createstudentsQuery/selectStudentsQuery somehow failed.
    echo "Could not create students table";
} else{
    echo "Students table ist working.";
}

//array missingParameters wird angelegt
$missingParameters = array();

//hier wird überprüft ob etwas gepostet wurde
if(isset($_POST['firstName'])&&isset($_POST['lastName'])&&isset($_POST['semester'])&&isset($_POST['course'])&&isset($_POST['grade'])){

//wenn inputs nicht leer sind, wird die Eingabe einer Variable übergeben
if (!empty($_POST['firstName'])){
    $firstName = $_POST['firstName'];
}else{
    $missingParameters [] = 'firstName';
}if (!empty($_POST['lastName'])){
        $lastName = $_POST['lastName'];
}else{
    $missingParameters [] = 'lastName';
}
if (!empty($_POST['semester'])){
    $semester = $_POST['semester'];
}else{
    $missingParameters [] = 'semester';
}
if (!empty($_POST['course'])){
    $course = $_POST['course'];
}else{
    $missingParameters [] = 'course';
}
if (!empty($_POST['grade'])){
    $grade = $_POST['grade'];
}else{
    $missingParameters [] = 'grade';
}

/**wenn das array missingParamater nicht leer ist, also eine Eingabe fehlt*/
if (!empty($missingParameters)) {
    echo "Please enter an information.";
    //array missingParameters wird in der Variable responseArray gespeichert
    $responseArray = array("missing parameters"=>$missingParameters);
    //und mit der Methode json_encode als json ausgegeben
    echo json_encode($responseArray);

/**wenn die Eingaben vollständig sind, werden die Werte in der Tabelle gespeichert*/
} else {
    $insertstudentsQuery = "INSERT INTO students(firstName, lastName, semester, course, grade)
    VALUES('$firstName', '$lastName', '$semester', '$course', '$grade')";

    $insertResult = mysqli_query($c, $insertstudentsQuery);

    //This tells the client that the data was successfully inserted.
    $responseArray = array("message"=>"OK");
    echo json_encode($responseArray);

}

} ?>

【问题讨论】:

    标签: php arrays json mysqli


    【解决方案1】:

    这几乎肯定会发生,因为:

    1. JSON 格式错误,和/或
    2. 返回空字符串而不是 JSON

    开始调试的一种方法是......执行以下操作:

    if (json_received =="") json_received = "{}";
    

    这提供了 {}(空对象)的临时最低公分母 JSON 值

    如果它开始工作,您将 100% 确定它是格式错误的 JSON。

    其他需要考虑的事项:

    1. 您的 MySQL 数据库格式是否设置为 UTF-8?
    2. 在将 $responseArray 传递给 JSON 解析/编码函数之前,您是否尝试过记录它?

    【讨论】:

    • 您好 InfiniteStack,谢谢您的回答!我发现我必须删除“回声”,现在 Json 工作正常。感谢您的帮助!
    • 很高兴知道 :) 看起来它需要 JSON 格式的输出,但里面有常规的字符串文本。
    猜你喜欢
    • 2015-03-05
    • 1970-01-01
    • 2016-05-16
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多