douhuigan8063 2018-05-11 05:27
浏览 62

如何在ajax成功函数中访问Php返回json编码的数组元素

I have a vote system that is similar tho youtube vote system.once I sent the relevant data to the php function i want to get back the query results back to the ajax function.I have array of likes and dislikes .when I encoded the array ajax success function returns as follows. {"likes":2,"unlikes":0} I want to get likes and unlike values(2 and 1) to display in the html page. how can I get them.

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 () {

                }
            });
        }

vote.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);


    }

returned result from success function:{"likes":2,"unlikes":0}

  • 写回答

1条回答 默认 最新

  • duan198409 2018-05-11 05:33
    关注

    use this in your success funtion and you will get data

    result = $.parseJSON(result);

    now console.log(result) or alert(result)

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog