<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>

CC BY-SA 4.0
2018-09-26T00:32:12.810Z
2018-09-26T00:32:12.810Z
JamesNZ
<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>

<ajax><json>
Craft 3 Plugin Controller - returnJson() replacement?
# 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, which is what the \craft\web\Controller class extends (which is what your plugin controllers extend) there is a method asJson().
return $this->asJson($response);


---

