【发布时间】:2020-03-13 16:43:41
【问题描述】:
我们使用的是 Laravel 5.* 我们不想在我们的项目中更改此代码 响应()->json
它将反映整个应用程序,这太可怕了。所以我们需要一个合适的解决方案,我在这个问题的 cmets 中找到了解决方案。 谢谢。
这里是场景:
$response = response()->json(['code' => 1,
'message' => 'Content-type is ABC',
'data' => ['code'=>1,'message'=>'Content-type is ABC']
]);
echo json_decode($response); // It always print null and nothing.
dd($response); // print Null
return $response; //You will get proper json response in function return.
【问题讨论】:
-
那么,你还有什么问题?
-
尝试返回
json_encode(['code' => 1, 'message' => 'Content-type is ABC', 'data' => ['code'=>1,'message'=>'Content-type is ABC'] ]); -
就this post 而言,您应该可以打印它。
-
您的回复为空,这就是原因
-
dd($response->getContent());将打印 json 响应