简体   繁体   中英

Lithium return json response with request header accept:application/json

I have a lithium app and I try to return json response when the header request has Accept:application/json (I would like to avoid using the type parameter in my route)

I add this instruction in my app/bootstrap/media.php but I still have a html response

Media::type('json', array('application/json'), array(
    'conditions' => array('type' => true)
));

What did I miss ?

I saw a similar question but it seams that the framework evolved : PHP lithium(li3) how to set up automatic response with JSON

Because content negotiation involves some overhead, it is not enabled by default. To enable it, simply add the following to your controller:

protected function _init() {
    $this->_render['negotiate'] = true;
    parent::_init();
}

Also, you don't need the Media configuration, as JSON is configured by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM