为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复“Uncaught SyntaxError:

如何修复“Uncaught SyntaxError:

PHP
慕莱坞森 2021-06-22 17:14:10
我正在尝试json_encode在 php 代码上输出一个。然后我突然在谷歌浏览器的网络选项卡上发现了这个错误。这是我第一次处理 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:61each @ jquery.min.js:2(anonymous) @ script.js:60u @ jquery.min.js:2fireWith @ jquery.min.js:2k @ jquery.min.js:2(anonymous) @ jquery.min.js:2load (async)send @ jquery.min.js:2ajax @ jquery.min.js:2(anonymous) @ script.js:52dispatch @ jquery.min.js:2y.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            }        }) 老实说,我无法指出这里的错误,因为在我的知识水平上没有可理解的错误消息供我查看。
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

您解析收到的 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();

            }

        });

    });


查看完整回答
反对 回复 2021-06-25
  • 1 回答
  • 0 关注
  • 278 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信