【问题标题】:how to access the Php return json encoded array elements in the ajax success function如何在ajax成功函数中访问php返回json编码的数组元素
【发布时间】:2018-05-11 05:27:09
【问题描述】:

我有一个类似于 youtube 投票系统的投票系统。一旦我将相关数据发送到 php 函数,我想将查询结果返回给 ajax 函数。我有一系列喜欢和不喜欢的东西。当我编码后的数组ajax成功函数返回如下。 {“喜欢”:2,“不喜欢”:0} 我想在 html 页面中显示喜欢和不喜欢的值(2 和 1)。 我怎样才能得到它们。

user-likes.js

        function liked(answerId) {

            var anID = answerId;
            var clickedIcon = $('#Like');
            //var nextIcon=$('#like')

            $.ajax({
                dataType:'json',
                url: 'vote.php' ,
                type: 'post',
                data: {
                    'liked': 1,
                    'postid': anID

                },
                success: function (results) {


                    alert(results);

                    clickedIcon.removeClass();
                    clickedIcon.addClass('mdi-action-thumb-up teal-text');
                    //.text(res[0].likes);


                },
                error: function () {

                }
            });
        }

投票.php

    <? php include '../../backend/connection.php';
    session_start();
    $username = $_SESSION['username'];


    if (isset($_POST['liked'])) {
        $answerId = $_POST['postid'];
        $result = mysqli_query($connection, "SELECT * FROM answervote WHERE aID='" . $answerId . "'");
        $row = mysqli_fetch_array($result);
        $nUp =(int)($row['voteUpCount'])+1 ;

        $nDown=(int)$row['voteDownCount'];
        $law_query = mysqli_query($connection, "SELECT aUser FROM answer WHERE aID='" . $answerId . "'");
        $law = mysqli_fetch_array($law_query);
        $lawyer_name=$law['aUser'];

        $point_query=mysqli_query($connection,"SELECT points FROM lawyer  WHERE username='".$lawyer_name."'");
        $point_result=mysqli_fetch_array($point_query);

        $points=$point_result['points']+1;



        try{

            mysqli_autocommit($connection, FALSE);
            mysqli_query($connection,"UPDATE lawyer SET points='$points' WHERE username='".$lawyer_name."'");
            mysqli_query($connection, "INSERT INTO likes(username,aID) VALUES ('$username','$answerId')");
            mysqli_query($connection, "UPDATE answervote SET voteUpCount='$nUp' WHERE aID ='" . $answerId . "'");
            mysqli_commit($connection);
        }catch(Exception $e ){
            $connection->rollback();

        }
        $ratings=[
            "likes" => $nUp,
            "unlikes"=> $nDown

        ];
        echo json_encode($ratings);


    }

成功函数的返回结果:{"likes":2,"unlikes":0}

【问题讨论】:

  • 你试过results.likes/results.unlikes吗?
  • 先更新表格,然后得到结果,否则不包含当前投票。
  • 是我得到的输出是正确的。我认为编码后的数据结果应该是这样的。'[{"likes":2,"unlikes":0}]'
  • @Sean 成功了。谢谢。

标签: javascript php ajax


【解决方案1】:

在你的成功函数中使用它,你会得到数据

结果 = $.parseJSON(result);

现在 console.log(result) 或 alert(result)

【讨论】:

  • 当他们有dataType:'json',时真的需要$.parseJSON
  • @Sean - 不,不是。 jQuery 会在返回结果之前为您执行此操作。
  • results.likes/results.unlikes 并像这样访问结果
  • 别告诉我们。而是更新您的答案:-)
  • @GirishNinama 。我试过 alert(results.likes)。只有后端数据库正在更新。
猜你喜欢
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 2014-10-20
  • 2010-11-30
相关资源
最近更新 更多