问题描述
我在Laravel中的POST表单有一个奇怪的问题.
I'm having an odd issue with a POST form in Laravel.
在发送发布请求时,我的Laravel抛出MethodNotAllowedHttpException.在查看错误消息后,我可以看到Laravel认为我的请求是GET请求,但不是.
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数据和GET数据时,Laravel似乎认为它们都是空的.这让我有些困惑,因为似乎正在进行某种重定向,所以错误的HTTP_REFERER是我要发布的页面.
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.
我以前遇到过这个问题,在那里创建命名路由可以解决我的问题,但是我正在制作一个简单的CMS,因此使用了表单模板,并且在没有允许的情况下,我无法使用命名路由用户使用Blade语法是一个坏主意.
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.
我的路线如下(简化为"Hello world"):
My route is as follows (Simplified to a "Hello world"):
Route::post('/signup/add', function(){
echo "Hello world";
});
http://pastebin.com/EsAeyHFx <-完整路由.php
http://pastebin.com/EsAeyHFx <- Full routes.php
http://pastebin.com/ByHdUFcK <-我的表格.没什么,只输入纯文本/单选按钮.没有html或其他任何特殊内容.
http://pastebin.com/ByHdUFcK <- My form. Nothing fancy, only plain text/radiobuttons input. No html or anything special.
对此,更奇怪的部分是,我有另一种形式(登录形式),不会导致此行为.
The even more strange part on this, is that I have another form (login form) that does not result in this behavior.
我一直在寻找关于StackOverflow的其他几个问题,但它们似乎最终都是将POST数据发送到GET路由的错误.这不是我的情况.
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.
如果我将路由从POST更改为GET,则可以正常工作.
If I change the route from POST to GET, it works fine.
我还尝试将表单的操作更改为GET,并使用隐藏字段_method并将其设置为post-不成功.
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.
有人可以告诉我此异常是怎么回事以及如何解决该问题吗?
Can someone tell me what is going on with this Exception and how to fix it?
已添加:经过一些试验,我发现使用第3方软件(例如chrome扩展名Postman)并将POST请求发送到页面时,它可以按预期工作.
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.
推荐答案
好吧,经过一番挖掘,在jsfiddle中制作表格,使用jQuery等,我发现了问题!
Ok, after some digging around, making forms in jsfiddle, using jQuery and so on, I found the problem!
我的表单在action属性中有一个斜杠,据说这是不允许的.
My form has a trailing slash in the action attribute, which it supposedly is not allowed to.
解决方案只是将我的代码更改为action="/signup/add"
而不是action="/signup/add/
The solutions was simply to change my code to action="/signup/add"
instead of action="/signup/add/
老兄,我觉得很蠢...
Man, I feel stupid...
这篇关于Laravel POST表单上的MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!