如何修复“Uncaught SyntaxError:

我正在尝试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: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

            }

        })


 

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


慕莱坞森
浏览 279回答 1
1回答

慕慕森

您解析收到的 JSON 两次。通过您的$.each调用,您可以遍历对象并尝试解析每个对象键的值。在第一次迭代中,使用您的示例数据,您尝试将“1_5cdb7ad317291.jpeg”解析为 JSON,这不是有效的 JSON。因此,您可以在第一次解析后访问该值。$("[id^='deleteNotes']").click(function() {&nbsp; &nbsp; &nbsp; &nbsp; var id = $(this).closest('div').attr('id');&nbsp; &nbsp; &nbsp; &nbsp; console.log(id);&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "ajax/deleteidentifier.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "post",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id_delete: id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .done(function(result_query_sql_deletedStatus_notes){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data = JSON.parse(result_query_sql_deletedStatus_notes);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data.deleted_status == "n") { //the cursor points me here. precisely, at the end of JSON and start of .parse&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Moved to deleted folder.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.reload();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Note permanently deleted!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.reload();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP