繁体   English   中英

SyntaxError: JSON.parse: JSON 第 1 行第 2 列的意外字符

[英]SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON

我需要将此 div 附加到另一个 div ,但它给了我这个错误:

SyntaxError: JSON.parse: JSON 数据第 1 行第 2 列的意外字符

这是我的 javascript 代码:

var str = {'message': message,'text': text};
$.ajax({
    type: "POST",
    url: "api/reply",
    data: str,
    dataType: "json",
    cache: false,
    success: function(response)
    {
        var respons = jQuery.parseJSON(response);
        var type = respons.status
        if (type == 'success') {
            $("<div></div>").html(respons.message).appendTo("#messages");
        }
        else
        {
            toastr.error(respons.message)
        }
    }
})

简单地改变

var respons = jQuery.parseJSON(response);

var respons = response;

说明:

如果您的 AJAX 调用的配置具有dataType: json您将获得一个 JavaScript 对象,因此不再需要使用 JSON.parse()。

这是解析 JSON 的一种骇人听闻的非正统方式,但是如果您仍然想从 AJAX 调用中使用JSON.parse()或者只是在已经解析且不是字符串的 JSON 上使用,您可以使用JSON.stringify()里面:

var respons = JSON.parse(JSON.stringify(response));

问题是您不是在解析字符串,而是在解析已解析的对象。

您对象中的值似乎未定义。 更改var str = {'message': message,'text': text}; var str = {message: 'message',text: 'text'};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM