weixin_33720956 2018-08-01 13:46 采纳率: 0%
浏览 23

Yii2提交与Ajax的链接

I'm trying to create votes for my content in Yii2, so I'm trying to insert data with Ajax using "a" tag and widget but with ajax it's not working , without ajax it's working perfect. but when I use Ajax it's not working and I have no errors in console. my code for view is :

<script type="text/javascript">
    $('document').ready(function(){
        $('#btn-vote-up' + <?= $pst_id ?>).on("click", function(e){
            $.ajax({
                type: "POST",
                data: {"value" : "Like"},
                success: function(msg) {
                    console.log (<?= $pst_id ?>);
                    $('#note-up' + <?= $pst_id ?>).load(' #note-up' + <?= $pst_id ?>);
                }               
            });
            e.preventDefault();
        });
    });
</script>


    <?= Html::a('<i class="ly-ic-favorite-plus"></i>', '#', [
                'class' => 'btn-logout',
                'id' => 'btn-vote-up'.$pst_id,
                'data'  => [
                    'params' => [
                        'value' => 'Like',
                        'pstIDL' => $pst_id,
                        ],
                    ],
        ]) ;
    ?>
    <?php yii\widgets\Pjax::begin(['id' => 'note-up'.$pst_id]) ?>
        <span><?= $total_up ?></span>
    <?php yii\widgets\Pjax::end() ?>

And my widget code is :

if (Yii::$app->request->post('value') == 'Like')
            {
                $pstIDL = Yii::$app->request->post('pstIDL');

                $modelLIKEPOST = $this->findLikePost($pstIDL);

                AxVotePost::VoteUP($modelLIKEPOST, $this->usr_rid, $this->chn_id, $pstIDL);
                header('Location: ' .Url::current() );
                exit;
            }
  • 写回答

1条回答 默认 最新

  • weixin_33713503 2018-08-01 15:10
    关注

    You are not sending the $pstIDL value when making an ajax call and only value is being sent, you can use var data=$(this).data('params') to get both pstIDL, and the value json and send it along the data in the call see below

    <script type="text/javascript">
        $('document').ready(function(){
            $('#btn-vote-up' + <?= $pst_id ?>).on("click", function(e){
               var data= $(this).data('params');
                $.ajax({
                    type: "POST",
                    data: data,
                    success: function(msg) {
                        console.log (<?= $pst_id ?>);
                        $('#note-up' + <?= $pst_id ?>).load(' #note-up' + <?= $pst_id ?>);
                    }               
                });
                e.preventDefault();
            });
        });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt安装后运行不了,这是我电脑的问题吗
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法