php - 如何修复 "Uncaught SyntaxError: Unexpected token _ in JSON at position . . . "

标签 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 值上的 deleted_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);
?>

最佳答案

您对接收到的 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();
            }
        });
    });

关于php - 如何修复 "Uncaught SyntaxError: Unexpected token _ in JSON at position . . . ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56208174/

相关文章:

php - symfony2 twig 和多级子文件夹

Javascript:Html2canvas 回调

mysql - JOIN 和 GROUP_CONCAT 三张表

php - MySQL多列总计解决方案

sql - WHERE 子句中的 IN 未使用 SQL::Statement 正确解析

php - 通过双美元符号访问对象变量

php - Guzzle 6 - Promises - 捕获异常

php - YouTube URL iframe问题

javascript - 如何在聊天刷新时将滚动条保持在底部,而不是转到中间

javascript - 使用 jQuery 更改 CSS 规则定义