Yii2 ajax 提交 view 端 和 controller端

来源:互联网 发布:算法的乐趣代码 编辑:程序博客网 时间:2024/06/03 20:32

view端

<?php use common\widgets\JsBlock;use yii\helpers\Url;?><li class="mb5">    <label class="sectionLabel-A1">test*:        <a href="#" id="test" style="color: #3B5999;font-weight: normal">添加视角</a>    </label>    <div id="addAngles" class="sectionBox-A1 clear sectionForm-A1 sectionForm-A1-4">    </div></li><?php JsBlock::begin() ?><script>var path = "<?php echo Url::toRoute('article/ajax',true); ?>";$('#test').click(function(){    $.ajax({             type: "POST",             url: path,             data:{                "Page": 1,                "Size": 8,                "Path": path,                "Children": false,                "Sort": "-Article__Time"             } ,             dataType: "json",             success: function(json){                 $("#addAngles").append(json);             }         })});</script><?php JsBlock::end() ?> 

controller 端

    public function actionAjax(){        if (Yii::$app->request->isAjax) {             $para = Yii::$app->request->post();            $searchname= explode(":", $para['Page']);            $b = $para['Path'];            $test = "ajax worked!";            return \yii\helpers\Json::encode($b);            // request is ajax request         }     }

结果显示

test*: 添加视角http://www.wanjia.com/frontend/web/index.php?r=article%2Fajax
原创粉丝点击