Answer the question
In order to leave comments, you need to log in
How to work with renderAjax correctly?
Good afternoon, I'm trying to figure out how to work with renderAjax.
And almost everything is clear, except how to work with the response
. For example, I have a form, I submit it, and in response I wait for Alert.
I do renderAjax('myalert');
here is the view of myalert:
<?php
use yii\bootstrap\Alert;
?>
<?php Alert::begin([
'options' => [
'class' => 'alert-danger',
],
'closeButton' => false,
]);?>
<h4 class="alert-heading">Ошибка!</h4>
<p>Ну и описание собственно.</p>
<?php Alert::end();?>
Answer the question
In order to leave comments, you need to log in
You need to send an ajax request to the server to process it and return a response. In order not to receive all html with layout, etc. on ajax requests. There is a renderAjax method. That is, the method does not do ajax, it gives data for ajax. He doesn't put anything in. You must insert in js in the place where you send the ajax request. Where is the code that sends the ajax request?
As I understand it, you just need some object in JSON.
renderAjax is the same as renderPartial, just includes css and js in the response.
And if you need a serialized response, then you need something like this:
public function actionAnyAction() {
…
return $this->asJson($anyObject);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question