Ð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)
试试这个:
改变var respons = jQuery.parseJSON(response);以var respons = response;
说明:
如果配置是dataType:json,你将获得一个javascript对象,不再需要JSON.parse