Yii2 Controller Ajax operation example, yii2ajax
This article describes the yii2 Controller Ajax operation method. We will share this with you for your reference. The details are as follows:
Public function actionSample () {if (Yii: $ app-> request-> isAjax) {$ data = Yii ::$ app-> request-> post (); $ searchname = explode (":", $ data ['searchname']); $ searchby = explode (":", $ data ['searchby']); $ searchname = $ searchname [0]; $ searchby = $ searchby [0]; $ search = // your logic; \ Yii :: $ app-> response-> format = \ yii \ web \ Response: FORMAT_JSON; return ['search' => $ search, 'code' => 100,];}
Template call Method
$. Ajax ({url: '<? Php echo Yii: $ app-> request-> baseUrl. '/supermarkets/sample'?> ', Type: 'post', data: {searchname: $ ("# searchname "). val (), searchby: $ ("# searchby "). val ()}, success: function (data) {console. log (data. search );}});