Laravel 8 JSON 响应
json 自动将 Content-Type 头信息设置为 application/json,同时使用 PHP 的 json_encode 函数将给定的数组转换为 JSON :
return response()->json([
'name' => 'Abigail',
'state' => 'CA',
]);如果想要创建 JSONP 响应,可以结合 withCallback 方法使用 json 方法:
return response()
->json(['name' => 'Abigail', 'state' => 'CA'])
->withCallback($request->input('callback'));