为什么此json Symfony输出输出标头 [英] Why this json Symfony output outputs the headers

查看:65
本文介绍了为什么此json Symfony输出输出标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我玩Symfony和drupal 8的第一天,所以如果我的问题很明显,请原谅。

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

使用drupal 7:

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

json输出是干净的:

the json output is clean:

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

使用drupal 8:

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

输出的标题如下:

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",....]}

如何摆脱这些标头?

我很想阅读参考文献此处

非常感谢任何提示。

推荐答案

您只能获得内容通过调用 $ response-> getContent()来进行响应。

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

您可以这样做

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

但是,请注意,这将是一个坏习惯,因为您将在此过程中丢失响应头,例如,不会告诉您响应的内容类型是什么(在本例中为 application / json)等...

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 ...

不知道如何正确处理drupal,不胜感激。

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

这篇关于为什么此json Symfony输出输出标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆