POST 表单上的 Laravel MethodNotAllowedHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29327047/
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
Laravel MethodNotAllowedHttpException on POST form
提问by Fizk
I'm having an odd issue with a POST form in Laravel.
我在 Laravel 中的 POST 表单有一个奇怪的问题。
When sending a post request, my Laravel throws an MethodNotAllowedHttpException. Upon looking into the errormessage, I can see that Laravel thinks that my request is a GET request, which it is not.
发送 post 请求时,我的 Laravel 抛出 MethodNotAllowedHttpException。查看错误消息后,我可以看到 Laravel 认为我的请求是一个 GET 请求,但事实并非如此。
When looking at both POST data and GET data of the errorpage, Laravel seems to think that they are both empty. This leaves me a bit confused, since it seems that some kind of redirect is going on, the HTTP_REFERER on the error is the page I'm posting from.
在查看errorpage的POST数据和GET数据时,Laravel似乎认为它们都是空的。这让我有点困惑,因为似乎某种重定向正在进行,错误中的 HTTP_REFERER 是我发布的页面。
I've had this issue before, where making a named route solved my problem, but I'm making a simple CMS, so templates for a form is used, and it's not possible for me to use named routes, without allowing the user to use Blade syntax which is a bad idea.
我以前遇到过这个问题,创建命名路由解决了我的问题,但是我正在创建一个简单的 CMS,因此使用了表单模板,并且我无法使用命名路由,而不允许用户使用 Blade 语法,这是一个坏主意。
My route is as follows (Simplified to a "Hello world"):
我的路线如下(简化为“Hello world”):
Route::post('/signup/add', function(){
echo "Hello world";
});
http://pastebin.com/EsAeyHFx<- Full routes.php
http://pastebin.com/EsAeyHFx<- 完整的 routes.php
http://pastebin.com/ByHdUFcK<- My form. Nothing fancy, only plain text/radiobuttons input. No html or anything special.
http://pastebin.com/ByHdUFcK<- 我的表格。没什么特别的,只有纯文本/单选按钮输入。没有 html 或任何特别的东西。
The even more strange part on this, is that I have another form (login form) that does not result in this behavior.
更奇怪的是,我有另一个表单(登录表单)不会导致这种行为。
I have been looking at several other questions on StackOverflow, but they all seem to end up being a mistake of sending POST data to a GET route. This is not my case.
我一直在查看有关 StackOverflow 的其他几个问题,但它们似乎最终都是将 POST 数据发送到 GET 路由的错误。这不是我的情况。
If I change the route from POST to GET, it works fine.
如果我将路由从 POST 更改为 GET,它工作正常。
I've also tried to change the action of the form to GET and use the hidden field _method and set it post - No success.
我还尝试将表单的操作更改为 GET 并使用隐藏字段 _method 并将其设置为 post - 没有成功。
Can someone tell me what is going on with this Exception and how to fix it?
有人能告诉我这个异常是怎么回事以及如何解决它吗?
ADDED: After some experimenting, I found out that when using 3rd party software (Like chrome extension Postman) and sending a POST request to the page, it works as inteded.
补充:经过一些试验,我发现当使用 3rd 方软件(如 chrome 扩展 Postman)并向页面发送 POST 请求时,它按预期工作。
回答by Fizk
Ok, after some digging around, making forms in jsfiddle, using jQuery and so on, I found the problem!
好的,经过一番挖掘,在 jsfiddle 中制作表单,使用 jQuery 等,我发现了问题!
My form has a trailing slash in the action attribute, which it supposedly is not allowed to.
我的表单在 action 属性中有一个尾部斜杠,这应该是不允许的。
The solutions was simply to change my code to action="/signup/add"
instead of action="/signup/add/
解决方案只是将我的代码更改action="/signup/add"
为action="/signup/add/
Man, I feel stupid...
老兄,我觉得自己很傻……
回答by ushi-deshi
The MethodNotAllowedHttpException error means that the verb of the query is not consistent with the expected routes.
MethodNotAllowedHttpException 错误意味着查询的动词与预期的路由不一致。
Do a php artisan route: listfor listing routes and look in development tools
Do a php artisan route: list用于列出路由并查看开发工具