在 Laravel POST 请求上重定向 302

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30297607/
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 11:34:32  来源:igfitidea点击:

Redirect 302 on Laravel POST request

phpapilaravel

提问by Bellots

I'm developing a Laravel Web Service. When I try my POSTroutes with web forms, everything works fine, but when I try the same with a REST Client like Postmanit doesn't get me the response that should.

我正在开发一个Laravel Web Service. 当我POST使用 Web 表单尝试路由时,一切正常,但是当我使用 REST 客户端尝试相同时,Postman它并没有得到我应该的响应。

It gives me status code 302, and redirects to "/". What's the problem?

它给了我状态代码302,并重定向到“/”。有什么问题?

采纳答案by Imtiaz Pabel

it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'

看起来在邮递员中,您应该指出您发送的数据是“x-www-url-formurlencoded”

回答by KeepChen

When you try to POST request by Postman,you need to add _token field and it's value which is used to be protect XSS attack.See https://laravel.com/docs/5.2/routing#csrf-protection

当您尝试通过 Postman POST 请求时,您需要添加 _token 字段及其值,用于保护 XSS 攻击。参见https://laravel.com/docs/5.2/routing#csrf-protection

回答by digembok

When visiting the endpoint using Postman, set the Header

使用 Postman 访问端点时,设置 Header

Accept: application/json

Accept: application/json

or Laravel would never know it's an API client and thus redirect with 302 message. It's the mechanism for latest Laravel version since they provide api.php router beside web.php router.

或者 Laravel 永远不会知道它是一个 API 客户端,因此使用 302 消息重定向。这是最新 Laravel 版本的机制,因为它们在 web.php 路由器旁边提供了 api.php 路由器。

回答by achasinh

This might be useful for other users that having the same problem cannot solve it with the solution above:

这对于使用上述解决方案无法解决相同问题的其他用户可能很有用:

Make sure that the form fields names for instance <input type="text" name="document_name">(document_name), match the names of the rules fields declared in the model.
public static $rules = ['document_name' => 'required|string'];
It doesn't throw any errors, nothing in the logs, it just redirects to the form, therefore the difficulty to find the problem.

确保表单字段名称例如<input type="text" name="document_name">(document_name)与模型中声明的规则字段的名称匹配。
public static $rules = ['document_name' => 'required|string'];
它不会抛出任何错误,日志中没有任何内容,它只是重定向到表单,因此很难找到问题。

回答by Tayyab Hussain

Http status code 302 is used for redirection. It can be due to server side problem, Please check your laravel project's error log fileor apache's error log file.

Http 状态码 302 用于重定向。这可能是由于服务器端问题,请检查您的Laravel 项目的错误日志文件apache 的错误日志文件。