Answer the question
In order to leave comments, you need to log in
How to correctly implement ajax response in yii2?
I have a form
$form = ActiveForm::begin([
'id' => 'emailsimple',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'validationUrl' => '/site/controllers/site/feedback-emailsimple',
]);
public function actionFeedbackEmailsimple()
{
Yii::$app->Url->RequestAjax__Check();
$model = new Emailsimple();
if ($model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
} else {
throw new ForbiddenHttpException;
}
}
Answer the question
In order to leave comments, you need to log in
'Js events can't be caught because the page is reloading...' - do not reload the page, but send form data via ajax, like this
datastring = $("form").serialize();
$.ajax({
url: "/controller/",
data: datastring,
type: "GET",
dataType: "json",
success: function(data){
},
error: function(data){
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question