Good afternoon. Faced json. Not strong here, I revised the code more than once, tried it differently, I registered it without variables, without processing. Data is sent to the server. The method processes correctly and $json['total'] - has the right value. But success: function(data) does not work. Made check on $.ajaxSetup - Requested JSON parse failed. what's wrong - I can not understand (

 public function like(){ if($_POST['track_id']) { $json = array(); $liked = $this->model->like($_POST['track_id']); $res = $this->model->count_like($_POST['track_id']); $json['total'] = (int)$res[0]['count_like']; echo json_encode($json); } } function addLike(track_id) { $.ajax({ type: 'post', url: '/tracks/like', data: 'track_id=' + track_id, dataType : "json", success: function (data) { alert("1"); } }); } 
  • {"total": 14} the answer in the firebag is correct. Status 200 OK 69ms - Alexandr Malyita

2 answers 2

You can check if there is anything else after your function. Just redo the last line of the query on

 exit(json_encode($json)); 

If after that you have an error - something is displayed before exit, you need to search through the code. This is the most obvious option.

  • success earned) - Alexandr Malyita
  • then I do not understand, echo is superfluous, but this is not logical - Alexandr Malyita

Miracles do not happen - if an error occurs. Requested JSON parse failed - it means that the server really gave something that is not a correct json.

Did you view the result of the query in the developer’s tools in the browser? The first assumption that came to my mind was that PHP issued some warning that violated the validity of the answer.