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

问题描述:

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

I need to append this div to another div , but it give me this error :


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

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

这是我的javascript代码:

This is my javascript code:

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); to var respons = response;

说明:

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

If the configuration is dataType: json, you'll get a javascript object is no longer necessary JSON.parse