我正在尝试通过post方法调用视图,但没有结果。通过ajax或actionPOST发送POST请求不会执行视图。 我需要一个通过post方法运行我的视图的示例。
Code below
<script>
function sendPost() {
$.ajax({
type: "post",
url: "<?php echo yii::$app->geturlmanager()->createurl('site/upload'); ?>",
data: {name:'Keth',_csrf : '<?=Yii::$app->request->getCsrfToken()?>'},
datatype: "json",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
return true;
}
</script>
controller site
public actionPost()
{
$name = 'Bart';
Yii::$app->runAction(['site/upload','name' => $name]);
}
public function actionUpload()
{
if (Yii::$app->request->isAjax) {
$name = Yii::$app->request->post('name');
}else
{
$name = Yii::$app->request->post('name');
return \yii\helpers\Json::encode($flag);
}
$model = new SiteUploadForm();
return $this->render('site-upload', [
'model' => $model,
'name' => $name ]);
}
view site
echo $name
答案 0 :(得分:1)
您的函数声明错误,请更改:
public actionPost()
{
$name = 'Bart';
Yii::$app->runAction(['site/upload','name' => $name]);
}
为:
public function actionPost()
{
$name = 'Bart';
Yii::$app->runAction(['site/upload','name' => $name]);
}
IDE应将此标记为无效。