简体   繁体   English

Uncaught SyntaxError: Unexpected token < in JSON at position

[英]Uncaught SyntaxError: Unexpected token < in JSON at position

I am currently trying to make a system of "favorites".我目前正在尝试制作一个“收藏夹”系统。 It works correctly but in the console of my browser gives an error (image below) and I do not understand why it happens.它工作正常,但在我的浏览器控制台中出现错误(下图),我不明白它为什么会发生。
I tried to find solutions but I can not understand them at all.我试图找到解决方案,但我根本无法理解它们。 Can anyone help me?谁能帮我?

index.php : index.php

        <div class="blog-fav">
            <i <?php if (userLiked($post['id'])): ?>
          class="fas fa-heart favorite like-btn" 
      <?php else: ?>
          class="fas fa-heart single like-btn"
      <?php endif ?>
      data-id="<?php echo $post['id'] ?>"></i>
        </div>

scripts.js : scripts.js

    $(document).ready(function(){
    
    $('.like-btn').on('click', function(){
      var post_id = $(this).data('id');
      $clicked_btn = $(this);
      if ($clicked_btn.hasClass('single')) {
        action = 'like';
      } else if($clicked_btn.hasClass('favorite')){
        action = 'unlike';
      }
      $.ajax({
        url: 'index.php',
        type: 'post',
        data: {
            'action': action,
            'post_id': post_id
        },
        success: function(data){
            res = JSON.parse(data);
            if (action == "like") {
                $clicked_btn.removeClass('favorite');
                $clicked_btn.addClass('single');
            } else if(action == "unlike") {
                $clicked_btn.removeClass('single');
                $clicked_btn.addClass('favorite');
            }
        }
      });       
    });
  });

The error I get:我得到的错误:

ERROR MESSAGE - IMG错误信息 - IMG

The problem is in the excerpt res = JSON.parse (data);问题出在摘录res = JSON.parse (data); , the return from AJAX is HTML, but you are treating it as JSON. ,从 AJAX 返回的是 HTML,但您将其视为 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM