L
L
lolka022019-04-11 10:15:12
Yii
lolka02, 2019-04-11 10:15:12

Why does yii2 sometimes return non-json format in ajax request?

When getting return result of ajax request

$response  = [..];
Yii::$app->response->format = Response::FORMAT_JSON;
return $response;

returns
<![CDATA[YII-BLOCK-HEAD]]><![CDATA[YII-BLOCK-BODY-BEGIN]]>
then json The data
because of this, I can not display the answer through Jquery, since the answer is not in json format. how to get rid of this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Moses Fender, 2019-04-11
@mosesfender

This indicates that $response is not what is expected, and the application returns some kind of exception. Moreover, he is trying to give a template that is made a little against the rules. Most likely in /views/layouts/main.php the template is made without $this->beginPage(), $this->beginBody(), and the like.
I recommend using a controller inherited from \yii\rest\Controller for Ajax data, and not from \yii\web\Controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question