简体   繁体   English

我在哪里放$ this-> request-> headers('Content-Type','application / json');

[英]Where do I put $this->request->headers('Content-Type', 'application/json');

I'm trying to change the content-type to application/json in Kohana. 我正在尝试将内容类型更改为Kohana中的application / json。 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. 但是,请求仍然是text / html内容类型。

Where should I put $this->request->headers('Content-Type', 'application/json'); 我应该在哪里放置$this->request->headers('Content-Type', 'application/json'); ?

To elaborate on Claudio's answer, yes you need to set the response header, not the request, like so 要详细说明Claudio的答案,是的,你需要设置响应头,而不是请求,就像这样

$this->response->headers('Content-Type','application/json');

Also, I'm not sure how you've implemented your controller, but it looks like it may be a template controller based on 另外,我不确定你是如何实现控制器的,但看起来它可能是基于的模板控制器

$this->content = json_encode($json_data);

If you are using a template controller, make sure you set auto_render to FALSE. 如果您使用的是模板控制器,请确保将auto_render设置为FALSE。

Finally, set the response body with your json data 最后,使用您的json数据设置响应正文

$this->response->body(json_encode($json_data));

The OP asked where to put it. OP问到哪里放。 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. 如果您正在使用扩展Controller_Template的控制器,就像我一样,我只是将Andrew Schmid的代码示例添加到我的基本控制器的after()方法(在parent :: after()之前)并且它工作得很好。

So: 所以:

Controller_Your_Controller extends Controller_Template {

   // Your controller actions

   public function after()
   {
       // Set the response content-type here
       $this->response->headers('Content-Type','application/json');
       parent::after();
   }
}

Well, you need to edit the response headers. 那么,您需要编辑响应标头。

http://kohanaframework.org/3.1/guide/api/Response#headers http://kohanaframework.org/3.1/guide/api/Response#headers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kohana 3.2 request-> headers ['Content-Type']一直失败 - Kohana 3.2 request->headers['Content-Type'] keeps failing 具有JSON内容类型的PUT请求 - PUT request with json content-type 如何为整个Zend Framework应用程序设置/更改内容类型标头 - How do I set/change content-type headers for an entire Zend Framework Application 如何在此请求中添加Content-Type application / json - How to add Content-Type application/json in this request 未记录的PHP自动将content-type:application / json解码为$ _REQUEST - PHP undocumented automatically decode content-type:application/json to $_REQUEST JAVA-处理针对Content-Type的HttpClient请求:application / json - JAVA - Process HttpClient request for Content-Type: application/json 为什么 axios 不处理 'Content-Type': 'application/json' - Why axios do not handle with 'Content-Type': 'application/json' $ this-> request-> is(array('put'))在cakephp框架中不起作用 - $this->request->is(array('put')) not working in cakephp framework Ajax中的JSON字符串发布请求内容类型application / formdata与application / json - JSON string in ajax post request content-type application/formdata vs application/json 应该什么时候Content-Type是application / json - When should Content-Type be application/json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM