Monday, March 20, 2017

MethodNotAllowedHttpException for Post Request in Laravel

I've two ajax post request called signIn and signOut in Laravel 5.4 .

I'm using jwt authentication (tymondesigns/jwt-auth) instead of csrf so I closed VerifyCsrfToken middleware line in Kernel.php

There are four routes in the project. `php artisan route:list:

Method  |URI     |Action                                    | Middleware 
GET|HEAD| /      |Closure                                   | web 
GET|HEAD| home   |Closure                                   | web
POST    | signOut|App\Http\Controllers\APIController@signOut| web 
POST    | signIn |App\Http\Controllers\APIController@signIn | web

Routes:

Route::get('/', function () {
    return view('welcome');
})->name('/');

Route::get('home', function () {
    return view('main');
})->name('home');

Route::post('signIn', 'APIController@signIn');
Route::post('signOut', 'APIController@signOut');

Process:

User sign in the account and then the home page is called in an AJAX request success via window.location.href='home' (127.0.0.1:8000/home).

The sign out button is pressed and signOut is called.

Sign Out Request:

 $.ajax({
            type: 'POST',
            url: 'signOut',
            data: data,
            dataType: 'JSON'
})

signIn is working perfectly but signOut send 500 error:

Request URL:http://127.0.0.1:8000/signOut
Request Method:POST
Status Code:500 Internal Server Error
Remote Address:127.0.0.1:8000

The description of error;

MethodNotAllowedHttpException in RouteCollection.php
in RouteCollection.php line 233
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 220
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 1

APIController:

..
use Illuminate\Http\Request;
..

public function signIn{..}

public function signOut{..}

I'm using Wamp in Windows 10.

I also tried other types of request but result is the same.

Do you have any advice?



via kbrk

Advertisement