A
A
AndrewSHkod2021-05-13 17:51:00
AJAX
AndrewSHkod, 2021-05-13 17:51:00

Why does it throw Uncaught SyntaxError: Unexpected end of JSON input?

js code:

$.ajax({
      url: 'index.php?route=product/product/review_like&product_id={{ product_id }}',
      type: 'post',
      data: {
        'action': action,
        'review_id': review_id
      },
      success: function(data){
      res = JSON.parse(data);
        alert('s');
        if (action == 'like') {
          $clicked_btn.removeClass('review-like-btn');
          $clicked_btn.addClass('review-liked-btn');
        } else if(action == 'unlike') {
          $clicked_btn.removeClass('review-liked-btn');
          $clicked_btn.addClass('review-like-btn');
        }
        // display the number of likes and dislikes
        $clicked_btn.siblings('span.review-likes-total').text(res.likes);
        $clicked_btn.siblings('span.review-dislikes-total').text(res.dislikes);

        // change button styling of the other button if user is reacting the second time to post
        $clicked_btn.siblings('.review-disliked-btn').removeClass('review-disliked-btn').addClass('review-dislike-btn');
      }
});


php code:
public function review_like() {
    $this->load->language('product/product');
    $json = array();

    if ($this->request->server['REQUEST_METHOD'] == 'POST') {
      
      // Captcha
      if ($this->customer->isLogged() && !empty($this->session->data['user_id'])) {
        $this->load->model('catalog/review');
        if(isset($this->request->post['action'])){
          $review_action = $this->request->post['action'];
          $review_id = $this->request->post['review_id'];
          $user_id = $this->session->data['user_id'];
          $review_total = $this->model_catalog_review->getTotalReviewsLikes($review_action, $review_id, $user_id);
          return json_encode($review_total);
        }
      }
    }
    
  }


Error: Uncaught SyntaxError: Unexpected end of JSON input

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2021-05-13
@AndrewSHkod

from the obvious: invalid json arrives.
but it's more likely that something else is falling out in front of him. starting from bom and ending with errors / other messages from the puff itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question