如何解决“未捕获的SyntaxError:位置... JSON中的意外令牌_”。

时间:2019-05-19 13:15:25

标签: php jquery sql json ajax

我正在尝试在php代码上输出json_encode。然后我突然在谷歌浏览器的网络标签上捕获了此错误。这是我第一次处理ajax和json中的错误。

Uncaught SyntaxError: Unexpected token _ in JSON at position 1
    at JSON.parse (<anonymous>)
    at String.<anonymous> (script.js:61)
    at Function.each (jquery.min.js:2)
    at Object.<anonymous> (script.js:60)
    at u (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at k (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)
(anonymous) @ script.js:61
each @ jquery.min.js:2
(anonymous) @ script.js:60
u @ jquery.min.js:2
fireWith @ jquery.min.js:2
k @ jquery.min.js:2
(anonymous) @ jquery.min.js:2
load (async)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
(anonymous) @ script.js:52
dispatch @ jquery.min.js:2
y.handle @ jquery.min.js:2

点击script.js:61会将我指向代码部分(由我在jquery代码中的注释所指向)

$("[id^='deleteNotes']").click(function() {
        var id = $(this).closest('div').attr('id');
        console.log(id);
        $.ajax({
            url: "ajax/deleteidentifier.php",
            type: "post",
            data: {
                id_delete: id
            }
        })
        .done(function(result_query_sql_deletedStatus_notes){
            $.each(JSON.parse(result_query_sql_deletedStatus_notes), function(key, value) {
                if (JSON.parse(value).deleted_status == "n") { //the cursor points me here. precisely, at the end of JSON and start of .parse
                    alert("Moved to deleted folder.");
                    window.location.reload();
                }
                else {
                    alert("Note permanently deleted!");
                    window.location.reload();
                }
            });
        });
    });

我想使用json_encode值上的deleteed_status值作为json警报的触发。这是json编码。

{
"note_record":"1_5cdb7ad317291.jpeg",
"note_type":"images",
"note_title":"",
"user_id":"9",
"date_created":"Wednesday, May 15, 2019. | 10:34:59 AM",
"date_updated":"",
"note_id":"1",
"archived_status":"n",
"deleted_status":"y",
"date_deleted":"Sunday, May 19, 2019. | 07:59:32 PM",
"image_directory":"image_uploads\/1_5cdb7ad317291.jpeg"
}

老实说,我无法在此处指出错误,因为在我的知识水平上没有可供我理解的错误消息。

编辑:这是在ajax中调用的deleteidentifier.php

<?php
    include_once("../db.php");
    include "../session.php";
    if (isset($_POST["id_delete"])) {
        $sql_deletedStatus_notes = "SELECT * FROM notes WHERE note_id = '". $_POST['id_delete'] ."'";
        $query_sql_deletedStatus_notes = $conn->query($sql_deletedStatus_notes);
        $result_query_sql_deletedStatus_notes = $query_sql_deletedStatus_notes->fetch_assoc();

        if ($result_query_sql_deletedStatus_notes["deleted_status"] == "y") {
            if ($result_query_sql_deletedStatus_notes["note_type"] == "notes") {
                $sql_delete_notes = "DELETE FROM notes WHERE note_id = '" . $_POST['id_delete'] . "'";
                $query_sql_delete_notes = $conn->query($sql_delete_notes);
            }
            else {
                $sql_delete_images = "DELETE FROM notes WHERE image_directory LIKE 'image_uploads/" . $_POST['id_delete'] . "' !_% ESCAPE '!'";
                $query_sql_delete_images = $conn->query($sql_delete_images);
            }
        }
        else {
            $sql_update_deletedStatus_notes = "UPDATE notes SET deleted_status='y', date_deleted = '" . $date_time_curr . "' WHERE note_id = '". $_POST['id_delete'] ."' OR image_directory LIKE 'image_uploads/" . $_POST['id_delete'] . "!_%' ESCAPE '!'";
            $query_sql_update_deletedStatus_notes = $conn->query($sql_update_deletedStatus_notes);
        }
    }
    $conn->close();
    echo json_encode($result_query_sql_deletedStatus_notes);
?>

1 个答案:

答案 0 :(得分:1)

您两次解析了接收到的JSON。通过$.each调用,然后遍历对象并尝试解析每个对象键的值。在第一个迭代中,使用示例数据,您尝试将“ 1_5cdb7ad317291.jpeg”解析为JSON,这不是有效的JSON。因此,您可以在第一次解析后访问该值。

$("[id^='deleteNotes']").click(function() {
        var id = $(this).closest('div').attr('id');
        console.log(id);
        $.ajax({
            url: "ajax/deleteidentifier.php",
            type: "post",
            data: {
                id_delete: id
            }
        })
        .done(function(result_query_sql_deletedStatus_notes){
            var data = JSON.parse(result_query_sql_deletedStatus_notes);
            if (data.deleted_status == "n") { //the cursor points me here. precisely, at the end of JSON and start of .parse
                alert("Moved to deleted folder.");
                window.location.reload();
            } else {
                alert("Note permanently deleted!");
                window.location.reload();
            }
        });
    });