本文實例講述了yii2使用ajax返回json的實現方法。分享給大家供大家參考,具體如下:
public function actionAjax(){ if(isset(Yii::$app->request->post('test'))){ $test = "Ajax Worked!"; // do your query stuff here }else{ $test = "Ajax failed"; // do your query stuff here } // return Json return /yii/helpers/Json::encode($test);}