Why this json Symfony output outputs the headers

百般思念 提交于 2019-12-23 02:29:27

问题


This is my first day to have fun with Symfony and drupal 8, so please excuse me if my question is very obvious.

With drupal 7:

drupal_json_output(array('products' => array_values($products)));
exit; 

the json output is clean:

{"products":["item_1","item_2",....]}

With drupal 8:

use Symfony\Component\HttpFoundation\JsonResponse; 
// some process
print new JsonResponse(array('products' => array_values($products)));
exit;

It outputs with the headers:

HTTP/1.0 200 OK
Cache-Control: no-cache
Content-Type:  application/json
Date:          Wed, 18 Jul 2012 07:53:26 GMT

{"products":["item_1","item_2",....]}

How do you get rid of those headers?

I am stuck to read the reference here.

Any hint is very much appreciated.


回答1:


You can get only the "content" of a response by calling $response->getContent().

In your case you could do

use Symfony\Component\HttpFoundation\JsonResponse; 
// some process
$response = new JsonResponse(array('products' => array_values($products)));
print $response->getContent();
exit;

However, be aware that this would be a bad practice because you would lose the response headers in the process, and wouldn't tell for example, what the content-type of you response is (in this case: "application/json") etc ...

I do not know how to do this properly with drupal, any tips is appreciated.



来源:https://stackoverflow.com/questions/11536952/why-this-json-symfony-output-outputs-the-headers

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