Posted in PHP onJuly 23, 2016
本文实例讲述了yii2控制器Controller Ajax操作的方法。分享给大家供大家参考,具体如下:
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, ]; } }
模板调用方法
$.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); } });
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
yii2控制器Controller Ajax操作示例
- Author -
懒人声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@