Laravel MethodNotAllowedHttpException on POST form

2019-06-21 06:14发布

问题:

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