A
A
alex4answ2019-02-18 15:25:31
Yii
alex4answ, 2019-02-18 15:25:31

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();?>

The answer comes with scripts, bootstrap and jquery, but they are already connected to the page, it turns out I can’t just take and paste the answer into the body of the page.
How to handle something like this?
I know that the question is simple, but I can not yandex it, everywhere is just an explanation of the difference between render and renderAjax

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2019-02-18
@alex4answ

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?

M
Moses Fender, 2019-02-18
@mosesfender

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 question

Ask a Question

731 491 924 answers to any question