Laravel从JSON响应中删除标头

时间:2017-10-23 15:42:26

标签: laravel laravel-5 laravel-4

我使用以下代码从函数返回Json编码的响应。

return response()->json($returnArray);

然而,响应如下,并包含HTTP标头:

Cache-Control: no-cache, private
Content-Type:  application/json
Date:          Mon, 23 Oct 2017 15:34:59 GMT

{"status":"success"}  

如何设置响应,以便不包含标题,只包含JSON?

{"status":"success"} 

2 个答案:

答案 0 :(得分:0)

怎么样:

return response()->toJson([
    'status' => 'success',
], 201);

或:

return Response::json(['data' => $array],201);

答案 1 :(得分:0)

我遇到了一些问题。

在我的情况下,问题出在方法中的string返回类型提示中。

示例:

public function getJson(): string{
    return response()->json(['foo' => 'bar']);
}

所以,我将string替换为JsonResponse并解决了所有问题。

也许会帮助别人。