2017-02-11 131 views
0

我的網絡路由正在工作,但是我無法發佈到我的api路由,我收到MethodNotAllowedHttpException。我認爲這是GET工作的csrf標記問題,但我無法弄清楚如何解決它。我正在使用郵差模擬API請求。Laravel 5.4 API路由

auth.php

'guards' => [ 
    'web' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 

    'api' => [ 
     'driver' => 'token', 
     'provider' => 'devices', 
    ], 
], 

RouteServiceProvider.php

protected function mapApiRoutes() 
{ 
    Route::middleware('api') 
     ->namespace($this->namespace) 
     ->group(base_path('routes/api.php')); 
} 

路由/ api.php

Route::post('api', ['uses' => '[email protected]']); 

kernel.php

protected $middlewareGroups = [ 
    'web' => [ 
     \App\Http\Middleware\EncryptCookies::class, 
     \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 
     \Illuminate\Session\Middleware\StartSession::class, 
     // \Illuminate\Session\Middleware\AuthenticateSession::class, 
     \Illuminate\View\Middleware\ShareErrorsFromSession::class, 
     \App\Http\Middleware\VerifyCsrfToken::class, 
     \Illuminate\Routing\Middleware\SubstituteBindings::class, 
    ], 

    'api' => [ 
     'throttle:60,1', 
     'auth:api', 
     'bindings', 
    ], 
]; 

這是從舊版本的Laravel升級而來的,我通過安裝Laravel 5.4的全新副本升級,然後複製到我的代碼中,根據需要進行更改。

+0

從哪裏和如何打電話?如果來自ajax共享該代碼? –

+0

@ detective404我正在使用郵遞員來打電話。我打電話給www.mydomain.com/api POST請求 – meeeee

+0

嗨@meeeee您是否找到了解決此問題的方法。我有類似的問題,如果它在你身邊解決,然後讓我知道。 –

回答

0

對不起,我在一個月前提出這個同樣的錯誤。問題是我沒有使用https。奇怪的是,我爲此得到了MethodNotAllowedException,我猜這讓我很失望。

+0

輝煌X'D愛的誠實:p – sourRaspberri

1

發送_token值這樣用POST請求

$.ajax({ 
     type: "POST", 
     url: "/your url", 
     data: {_token:$("input[name='_token']").val(),'other':'Other value'} 
    }).done(function(response) { 
     .... 
    }); 
+0

使用api時,這對我沒有意義。不應該需要令牌 – meeeee

+0

您正在調用哪個網址? –

+0

mydomain.com/api – meeeee