XF 2.0 Proper way to return JSON

Jean-Baptiste

Well-known member
Hello,

To return JSON responses from my Controller, I'm using the following code :
Code:
        header('Content-Type: application/json');
        echo json_encode($data);
        die();

What is the good way to return JSON ?

Thanks
 
Hello,

To return JSON responses from my Controller, I'm using the following code :
Code:
        header('Content-Type: application/json');
        echo json_encode($data);
        die();

What is the good way to return JSON ?

Thanks
i think this is bad way, in xf 1.5.x you can return json data through jsonEncodeForOutput function in ViewPublic:
Code:
class Dev_TestAddon_ViewPublic_Test extends XenForo_ViewPublic_Base
{
    public function renderJson()
    {
        return XenForo_ViewRenderer_Json::jsonEncodeForOutput($this->_params);
    }
}

So, xf 2.0 must have the similar function to return json data
 
I've been doing this:
PHP:
        $reply = $this->message('Success');
        $reply->setJsonParams(['a' => 1, 'b' => 2]);
        return $reply;

The only ugly part is that I don't care for the message.
 
Sorry to pull up an old thread, but I try this:
PHP:
    public function actionTest(){

        $this->setResponseType('json');

        return $this->view('Em8er\Auth:API', '', ["foo" => "bar"]);

    }

And the response I get is this:
Code:
{"status":"ok","html":{"content":""},"visitor":{"conversations_unread":"0","alerts_unread":"0","total_unread":"0"},"debug":{"time":0.1417,"queries":4,"memory":1.72}}

I'm not getting my foo:bar view parameter coming through.

Any advice?
 
Thanks, I am trying to follow the same for an Ajax call, but getting 400 bad Request from the server.

Can't the same strategy be used with an Ajax call?

JavaScript:
jQuery.ajax({
    type: "POST",
    url: "{{ link('portal-a/reload') }}",  // portal-a/reload is the route to the action class
    dataType:"json",
    success: function(data){
        alert(success);
    },
    error: function(xhr){
        alert("An error occured: " + xhr.status + " " + xhr.statusText);
    }
});


PHP:
namespace PortalA\Pub\Controller;


class PortalA extends \XF\Pub\Controller\AbstractController
{
    public function actionReload()
    {
        $viewParams = [
            'name' => "Test"
        ];

        return $this->view('PortalA\Pub\View\Test\PortalA', '', $viewParams);
    }
}

PHP:
namespace PortalA\Pub\View\Test;

class PortalA extends \XF\Mvc\View
{
    public function renderJson()
    {
        $this->setResponseType('json');
        return $this->params;
        // Also tried return $this->response;
    }
}
 
The renderJson method in the view will not be called if the response type isn't already set to json in the controller, so trying to set it there is pointless.

Move $this->setResponseType('json'); to the top of your controller action.
 
That part working fine, I got json response and can see them inside post request. But how can I show json data in teamplate? How xf script working?

Code:
 <a href="mypathToAction" class="myclass"
            title="mytitle"
            data-xf-click="bookmark-click"
            data-sk-bookmarked="addClass:is-bookmarked"
            data-sk-bookmarkremoved="removeClass:is-bookmarked">
            click button ( {{$myParam}} )
   </a>

How can i change val of $myParam inside template? Json request send my new data of this param, but I can't understand how to change it on fly)
 
Back
Top Bottom