Laravel 响应函数只返回 200?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41692898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:08:22  来源:igfitidea点击:

Laravel response function only return 200?

laravellaravel-5

提问by ChenLee

For example:

例如:

response()->json(['error' => 'invalid', 401]);
response()->json(['success' => 'success', 200]);

These two ways are all response 200.If I want to return 200 when success,but 401 when error happened.How can I achieve this.

这两种方式都是响应200。如果我想成功时返回200,但发生错误时返回401。我该如何实现。

回答by oseintow

The 401 or 200 should be the second argument of the json function like this

401 或 200 应该是这样的 json 函数的第二个参数

response()->json(['error' => 'invalid'], 401);
response()->json(['success' => 'success'], 200);

回答by ujwal dhakal

try this,

尝试这个,

return response()->setStatusCode()->json