doutangguali32556 2017-12-05 18:24
浏览 58
已采纳

Ajax无法显示来自php的json数据。 json格式有什么问题?

I have read all the related questions that reference to this topic, but still cannot find answer here.
So, php and ajax works great. The problem starts when i try to include json, between php and ajax, to passing data.
here is my ajax:

function likeButton(commentId, userId, sessionUserId) {

  // check if the comment belong to the session userId
  if(sessionUserId == userId) {
    alert("You cannot like your own comment.");
  }
  else if(sessionUserId != userId) {
    var like_upgrade = false;
    $.ajax({
      url: "requests.php",
      type: "POST",
      dataType: "json",
      data: {
        keyLike: "like",
        commentId: commentId,
        userId: userId,
        sessionUserId: sessionUserId,
        like_upgrade: like_upgrade
      },
      success: function(data) {
        var data = $.parseJSON(data);
        $("#comment_body td").find("#updRow #updComLike[data-id='" +commentId+ "']").html(data.gaming_comment_like);
        if(data.like_upgrade == true) {
        upgradeReputation(userId);
      }
      }
    });
  }
}

Note, that i try not to include this:

var data = $.parseJSON(data);

Also i tried with diferent variable like so:

var response = $.parseJSON(data);

and also tried this format:

var data = jQuery.parseJSON(data);

None of these worked.

here is requests.php file:

    if(isset($_POST['keyLike'])) {
    if($_POST['keyLike'] == "like") {
    $commentId = $_POST['commentId'];
    $userId = $_POST['userId'];
    $sessionUserId = $_POST['sessionUserId'];

    $sql_upgrade_like = "SELECT * FROM gaming_comments WHERE gaming_comment_id='$commentId'";
    $result_upgrade_like = mysqli_query($conn, $sql_upgrade_like);
    if($row_upgrade_like = mysqli_fetch_assoc($result_upgrade_like)) {
    $gaming_comment_like = $row_upgrade_like['gaming_comment_like'];
       }
    $gaming_comment_like = $gaming_comment_like + 1;

    $sql_update_like = "UPDATE gaming_comments SET gaming_comment_like='$gaming_comment_like' WHERE gaming_comment_id='$commentId'";
    $result_update_like = mysqli_query($conn, $sql_update_like);

    $sql_insert_like = "INSERT INTO gaming_comment_likes (gaming_comment_id, user_id, user_id_like) VALUES ('$commentId', '$userId', '$sessionUserId')";
    $result_insert_like = mysqli_query($conn, $sql_insert_like);
    $like_upgrade = true;
//json format
    $data = array("gaming_comment_like" => $gaming_comment_like,
     "like_upgrade" => $like_upgrade);
    echo json_encode($data);
    exit();
           }
        }

Note: i also try to include this to the top of my php file:

header('Content-type: json/application');

but still not worked.
What am i missing here?

  • 写回答

1条回答 默认 最新

  • duaeim2874 2017-12-05 18:29
    关注

    Don't call $.parseJSON. jQuery does that automatically when you specify dataType: 'json', so data contains the object already.

    You should also learn to use parametrized queries instead of substituting variables into the SQL. Your code is vulnerable to SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料