繁体   English   中英

Laravel:自定义 http 状态代码不起作用

[英]Laravel: custom http status code not working

Laravel 版本:5.5

我正在尝试从 laravel 控制器返回自定义 http 状态代码。 (使用 jQuery Ajax $.get() 调用这个 url)

在我的控制器功能中,我尝试了下面提到的两种方式,但它不起作用。

  1. 这个返回错误“方法 setStatusCode 不存在”。

     return response()->setStatusCode(202);
  2. 这个不是抛出错误而是总是返回200。

     $response = new Response(); $response->setStatusCode(202); $response->header('custom', 555); return $response;`

像这样使用它:

return response()->json("response content", 202);
//or
return response()->make("response content", 202);

检查https://laravel.com/api/5.0/Illuminate/Routing/ResponseFactory.html获取更详细的文档。

您可以使用return response()->json(['hello' => $value],201);

让我再介绍一些使用方法。 也许您正在寻找没有 json 的类似内容。

response(null)->setStatusCode(202);
response(null, 202);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM