Yii Framework Forum: Controller Action Should Return "application/json" - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Controller Action Should Return "application/json"

#1 User is offline   Urkman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 29-April 09
  • Location:Anrath/Germany

Posted 16 May 2014 - 02:10 AM

Hello,

how can I return "application/json" from my action and do not render a view?

Thanks,
Urkman
0

#2 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 639
  • Joined: 29-August 12

Posted 16 May 2014 - 02:42 AM

View PostUrkman, on 16 May 2014 - 02:10 AM, said:

how can I return "application/json" from my action and do not render a view?

Two methods to do this
Method 1:
Set the following in your control action before you return a render.
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

Method 2:
Instead of returning a render view in your controller action do the following:
echo \yii\helpers\Json::encode($content);

1

#3 User is offline   Urkman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 29-April 09
  • Location:Anrath/Germany

Posted 16 May 2014 - 03:03 AM

Thanks :)
0

#4 User is offline   slick1537 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 13-July 13

Posted 16 May 2014 - 07:23 AM

Useful stuff for me too. I was sending an AJAX response to the view and using 'echo' from there but I like this method better.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users