Laravel MethodNotAllowedHttpException on POST form

只谈情不闲聊 提交于 2019-12-12 10:59:35

问题


I'm having an odd issue with a POST form in Laravel.

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.

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.

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.

My route is as follows (Simplified to a "Hello world"):

Route::post('/signup/add', function(){
    echo "Hello world"; 
});

http://pastebin.com/EsAeyHFx <- Full routes.php

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.

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.

If I change the route from POST to GET, it works fine.

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?

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.


回答1:


Ok, after some digging around, making forms in jsfiddle, using jQuery and so on, I found the problem!

My form has a trailing slash in the action attribute, which it supposedly is not allowed to.

The solutions was simply to change my code to action="/signup/add" instead of action="/signup/add/

Man, I feel stupid...




回答2:


The MethodNotAllowedHttpException error means that the verb of the query is not consistent with the expected routes.

Do a php artisan route: list for listing routes and look in development tools



来源:https://stackoverflow.com/questions/29327047/laravel-methodnotallowedhttpexception-on-post-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!