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

Ðr *_*ade 2 javascript php ajax jquery 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)
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

Max*_*ard 12

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

var respons = JSON.parse(JSON.stringify(response));
Run Code Online (Sandbox Code Playgroud)


Alf*_* EM 7

试试这个:

改变var respons = jQuery.parseJSON(response);var respons = response;

说明:

如果配置是dataType:json,你将获得一个javascript对象,不再需要JSON.parse


Nal*_*ran 6

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