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)
}
}
})
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
the values in your object seem to be undefined. change var str = {'message': message,'text': text};
to var str = {message: 'message',text: 'text'};
Problem is you're not parsing a string, you're parsing an already parsed object.