Answers.Body: <p>I wasn't rtfm'ing hard enough. In the docs for <a href="https://www.yiiframework.com/doc/api/2.0/yii-web-controller#asJson()-detail" rel="nofollow noreferrer">\yii\web\Controller</a>, which is what the <strong>\craft\web\Controller</strong> class extends (which is what your plugin controllers extend) there is a method <strong><a href="https://www.yiiframework.com/doc/api/2.0/yii-web-controller#asJson()-detail" rel="nofollow noreferrer">asJson()</a></strong>.</p>
  
  <pre><code>return $this-&gt;asJson($response);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-26T00:32:12.810Z
Answers.LastActivityDate: 2018-09-26T00:32:12.810Z
Answers.UserDisplayName: JamesNZ
Body: <p>In Craft2, when making an ajax request to a plugin controller action, you would use the <strong>returnJson()</strong> method which is part of the <strong>BaseController</strong> class - which your plugin controllers extend.</p>
  
  <p>This sets some headers, echo's the response and kills any further processing.</p>
  
  <pre><code>public function actionDoStuff() {
  
      $response = (object)[
          "message" =&gt; "Look at me! I'm JSON!"
      ];
  
      $this-&gt;returnJson($response);
  }
  </code></pre>
  
  <p>In <strong>Craft 3</strong>/<strong>Yii 2</strong> I'm having trouble finding the equivalent.</p>
  
Tags: <ajax><json>
Title: Craft 3 Plugin Controller - returnJson() replacement?
ThreadText: # Craft 3 Plugin Controller - returnJson() replacement?
  
  (Asked by: username_0 on 2018-09-26)
  
  In Craft2, when making an ajax request to a plugin controller action, you would use the **returnJson()** method which is part of the **BaseController** class - which your plugin controllers extend.
  
  This sets some headers, echo's the response and kills any further processing.
  
  ```
  public function actionDoStuff() {
  
      $response = (object)[
          "message" => "Look at me! I'm JSON!"
      ];
  
      $this->returnJson($response);
  }
  
  ```
  
  In **Craft 3**/**Yii 2** I'm having trouble finding the equivalent.
  
  **Tags:** ajax, json
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-09-26. Score: 3
  
  I wasn't rtfm'ing hard enough. In the docs for [\yii\web\Controller](https://www.yiiframework.com/doc/api/2.0/yii-web-controller#asJson()-detail), which is what the **\craft\web\Controller** class extends (which is what your plugin controllers extend) there is a method **[asJson()](https://www.yiiframework.com/doc/api/2.0/yii-web-controller#asJson()-detail)**.
  
  ```
  return $this->asJson($response);
  
  ```
  
  
  ---
  
  