我该把 $this->request->headers('Content-Type', 'application/json'); 放在哪里
我正在尝试将 Kohana 中的内容类型更改为 application/json 。我将其放入控制器中的操作中:
$this->request->headers('Content-Type', 'application/json');
$this->content = json_encode($json_data);
但是,该请求仍然具有 text/html 内容类型。
我应该把 $this->request->headers('Content-Type', 'application/json'); 放在哪里?
I'm trying to change the content-type to application/json in Kohana. I put this in an action in my controller:
$this->request->headers('Content-Type', 'application/json');
$this->content = json_encode($json_data);
The request however, has still the text/html content-type.
Where should I put $this->request->headers('Content-Type', 'application/json');
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了详细说明克劳迪奥的答案,是的,您需要设置响应标头,而不是请求,就像这样
另外,我不确定您是如何实现控制器的,但看起来它可能是基于
如果您的 模板控制器使用模板控制器,请确保将 auto_render 设置为 FALSE。
最后,使用 json 数据设置响应正文
To elaborate on Claudio's answer, yes you need to set the response header, not the request, like so
Also, I'm not sure how you've implemented your controller, but it looks like it may be a template controller based on
If you are using a template controller, make sure you set auto_render to FALSE.
Finally, set the response body with your json data
那么,您需要编辑响应标头。
http://kohanaframework.org/3.1/guide/api/Response#headers
Well, you need to edit the response headers.
http://kohanaframework.org/3.1/guide/api/Response#headers
OP 问把它放在哪里。如果您使用扩展 Controller_Template 的控制器,就像我一样,我刚刚将 Andrew Schmid 的代码示例添加到我的基本控制器的 after() 方法中(在parent::after() 之前),并且效果很好。
所以:
The OP asked where to put it. If you're using a controller that extends Controller_Template, like I am, I just added Andrew Schmid's code example to my base controller's after() method (before parent::after()) and it worked great.
So: