Ðr Ssamade Ðr Ssamade - 4 months ago 23
Javascript Question

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 :


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


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)
}
}
})

Answer

Try this:

change var respons = jQuery.parseJSON(response); to var respons = response;

Explanation:

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