本文实例讲述了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);
}