简体   繁体   English

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

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

I need to append this div to another div , but it give me this error :我需要将此 div 附加到另一个 div ,但它给了我这个错误:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data SyntaxError: JSON.parse: JSON 数据第 1 行第 2 列的意外字符

This is my javascript code:这是我的 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)
        }
    }
})

Simply change简单地改变

var respons = jQuery.parseJSON(response);

to

var respons = response;

Explanation:说明:

If the configuration of your AJAX call is having dataType: json you'll get a JavaScript object so it's no longer necessary to use JSON.parse().如果您的 AJAX 调用的配置具有dataType: json您将获得一个 JavaScript 对象,因此不再需要使用 JSON.parse()。

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

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

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

the values in your object seem to be undefined.您对象中的值似乎未定义。 change var str = {'message': message,'text': text};更改var str = {'message': message,'text': text}; to var str = {message: 'message',text: 'text'};var str = {message: 'message',text: 'text'};

暂无
暂无

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

相关问题 SyntaxError:JSON.parse:JSON数据第3行第1列的意外字符 - SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data SyntaxError:JSON.parse:JSON数据的第1行第2列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符吗? - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data? 语法错误json.parse json数据第1行第1列的意外字符 - syntaxerror json.parse unexpected character at line 1 column 1 of the json data 语法错误:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data JSON错误:SyntaxError:JSON.parse:JSON数据的第2行第1列出现意外字符 - JSON error : SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data 我不断收到此错误SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符 - I keep getting this error SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data jQuery ajax语法错误json.parse json数据的第1行第1行出现意外字符 - Jquery ajax syntaxerror json.parse unexpected character at line 1 column 1 of the json data “ SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符”。Django频道 - “SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data”.Django Channels 出现错误:未捕获的 SyntaxError:JSON.parse:JSON 数据的第 3 行第 1 列出现意外字符 - 在这个几乎有效的代码中 - Getting error: Uncaught SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data - In this almost valid code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM