What is the error when using Ajax?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
8 views
When you access a server is issued in the console this message:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
What could be the reason?

The server that handles this request:
<?php/** Получение страницы*/function add_comment(){global $connection;$comment_author = trim(mysqli_real_escape_string($connection, $_POST['commentAuthor']));$comment_text = trim(mysqli_real_escape_string($connection, $_POST['commentText']));$parent = (int)$_POST['parent'];$comment_product = (int)$_POST['productId'];// если нет ID товараif(!$comment_product){$res = array('answer' => 'Unknown product!'); return json_encode($res); } // if fields are not filled if(empty($comment_author) OR empty($comment_text)){ $res = array('answer' => 'All fields must be zapaleniu'); return json_encode($res); } } ?>


The Ajax request:
$.ajax({ url: '<?=PATH?>add_comment', type: 'POST', data: {commentAuthor: commentAuthor, commentText: commentText, parent: parent, productId: productId}, success: function(res){ var result = JSON.parse(res); console.log(result); }, error: function(){ alert('Error JQ user'); }
by | 8 views

2 Answers

0 like 0 dislike
You have a piece of hypertext markup language in front of Jason, of course it will error:
5ae8743449ea6289483173.png

You POST did not even reach to the desired script. You have in PATH is localhost/catalog, and this means that the request is sent to the address
http(s)://adress-site.com/put-stranicy-na-kotoroj-Ty-delaesh-svoi-ax/localhost/catalog/add_comment

But I think it should be something like http(s)://address-of the website.com/catalog/add_comment, right? Correct, instead of the PATH was inserted /catalog/
by
0 like 0 dislike
if(!$comment_product){ $res = array('answer' => 'Unknown product!'); return json_encode($res); } // if fields are not filled if(empty($comment_author) OR empty($comment_text)){ $res = array('answer' => 'All fields must be zapaleniu'); return json_encode($res); }


Where is the sending of the result if everything is correct? Neither one of if not shot - what to bring to the server? By the way the empty string is not a valid janam.
But for simplicity, print in the console on the client res TO attempt parsing.
by

Related questions

0 like 0 dislike
2 answers
asked Apr 9, 2019 by kiselev-webmaster
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
35,405 users