John Davidson

PHP Yii controller render shows JSON instead of actual page

0 comments
Message:


I have a controller with 2 actions - home and run.
home shows a form rendered with a component.
The form submit goes to action run which sends a message outside(this part works OK).
I want to render the same page as in home again in action run.
But the same rendering code that works in home - in run shows a JSON in the browser.


This is the run function of both actions:


        public function run()
{
header('Content-type: application/json');

$this->controller->layout = "templates.layouts.blank";

...Some useful business logic things

$this->controller->render("templates.rulesrun.home", array(
"params" => array()
));
}

The result in the browser looks like this:


{"page":"HTML of my page","title":"Title of my page"}


PHP version 7.2
Yii version 1.1


Thank you all.



My Answer:
> It seems like the issue might be with the `header('Content-type: application/json');` line in your `run()` function. This header is telling the browser to expect JSON data, which is why you are seeing JSON instead of the actual page.

To render the same page as in the `home` action in the `run` action, you can remove the `header('Content-type: application/json');` line from the `run()` function. This will allow the page to be rendered as HTML instead of JSON.

Your `run()` function should look like this:

php
public function run(){
// Your code here
$this->render('home'); // Render the 'home' view
}


By removing the `header('Content-type: application/json');` line, the page should now be rendered as HTML in the browser when the `run` action is called.

Rate this post

4 of 5 based on 9083 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us