[英]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.