PHP lithium(li3) how to set up automatic response with JSON

好久不见. 提交于 2019-12-24 00:13:36

问题


I have a lithium app set up that way, so when

return($data) 

is used it either can be used in the lithium view.html.php as

echo $data

or if request header "accept" equals "json/javacript" it would return something like

{
data: { a:'b' }
}

automatically.

Unfortunately in the new app that I made as a test app that flow is not happening (and only HTML is always returned).

After doing a little research it seems like that it is supposed to be done automatically if I uncomment

require __DIR__ . '/bootstrap/media.php';

inside bootstrap.php But that didn't work, I still have HTML returned. I downloaded a recent version of the lithium framework(I downloaded it may be 1 or 2 months ago)

Anybody knows if automatic response with JSON requires some set up or not?


回答1:


taken from http://dev.lithify.me/lithium/tickets/view/353

which is then taken from the lithium docs

To enable automatic content-type negotiation (i.e. determining the content type of the response based on the value of the HTTP Accept header), set the 'negotiate' flag to true. Otherwise, the response will only be based on the type parameter of the request object (defaulting to 'html' if no type is present in the Request parameters)

http://li3.me/docs/lithium/action/Controller::$_render

If you need more help on how to implement leave a comment.




回答2:


It is also possible to set type to $this->request->accepts() when calling render().

return $this->render(array('type' => $this->request->accepts()));


来源:https://stackoverflow.com/questions/9756471/php-lithiumli3-how-to-set-up-automatic-response-with-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!