JSON.parse кидает ошибку unexpected non-whitespace character after JSON
Есть код аякс
$.ajax({ url: './',
type: 'POST',
data: {comment_author: comment_author, comment_text: comment_text, parrent: parrent, film_id: film_id},
success: function(res){
var result = JSON.parse(res);
console.log(result);
},
error: function(){
alert("Ошибка!");
}
});
Есть функция в модели, которая возвращает в контроллере строку JSON
function add_comment(){
global $dbh;
$comment_author = $_POST['comment_author'];
$comment_text = $_POST['comment_text'];
$parent = (int) $_POST['parrent'];
$film_id = (int) $_POST['film_id'];
if(!$film_id){
$res = array('result' => 'Неизвестный фильм');
return json_encode($res);
}
if(empty($comment_author) OR empty($comment_text)){
$res = array('result' => 'Не заполнены поля');
return json_encode($res);
}
$query = "INSERT INTO comments (comment_author, comment_text, parent_id, comment_film_id) VALUES('$comment_author', '$comment_text', $parent, $film_id)";
$res = $dbh->query($query);
$count = $res->rowCount();
if($count){
$res = array('result' => 'Все ок');
echo json_encode($res);
}else{
$res = array('result' => 'Ошибка');
echo json_encode($res);
}
}
Но консоль выдает ошибку
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 106 of the JSON data
Не могу найти проблему
Источник: Stack Overflow на русском