javascript 未捕获的语法错误:意外的标记 o

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31211424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:27:57  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected token o

javascriptjqueryjson

提问by Mike

I have a web app I am working on:

我有一个正在开发的网络应用程序:

$("#post").click(function () {

    var u = $('#u').val();
    var j = $('#j').val();

    $.post("http://www.myweb.php", {
            u: u,
            j: j
        })
        .done(function (data) {


            var obj = jQuery.parseJSON(data);
            alert(obj.status );
            //alert("Data Loaded: " + data);
        });

});

When it tries to retrieve the JSON I get:

当它尝试检索 JSON 时,我得到:

Uncaught SyntaxError: Unexpected token o

回答by Pointy

You don't have to call .parseJSON(). Your response has already been parsed. You're getting that error because the object you pass to jQuery.parseJSON()is being converted to the string "[object Object]". The unexpected token is that "o" in "object".

你不必打电话.parseJSON()。您的回复已被解析。您收到该错误是因为您传递给的对象jQuery.parseJSON()正在转换为 string "[object Object]"。意外的标记是“object”中的“o”。