gpt4 book ai didi

php - Laravel POST请求错误405 : MethodNotAllowedHttpException

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:25:36 25 4
gpt4 key购买 nike

在本地主机上使用 postman ,我能够在我的 API 上成功使用 POST。但是当部署到服务器并使用 postman 时,我总是得到错误 405:MethodNotAllowedHttpException,即使 POST 请求是针对路由声明的,从中间件中获取了我们的 verifycsrf,并取出了 web 中间件。还关注了 barryvdh 的 laravel 项目的 CORS 安装。

想知道是否有人有任何解决此问题的建议?

内核:

    namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
'Barryvdh\Cors\HandleCors',

];

/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],

'api' => [
'throttle:60,1',
],

'admin' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\PermissionAdminMiddleware::class,
],

];

/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
}

cors.php

    return [
/*
|--------------------------------------------------------------------------
| Laravel CORS
|--------------------------------------------------------------------------
|
| allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
| to accept any value.
|
*/
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['*'],
'allowedMethods' => ['*'],
'exposedHeaders' => [],
'maxAge' => 5,
'hosts' => [],
];

app.php 包括 Barryvdh\Cors\ServiceProvider::class,

路由.php

       Route::group(

[
'prefix' => 'api',
'middleware' => ['cors']
],

function()
{
Route::post('dummy', 'API\UsersController@dummy');

}
);

用户 Controller .php

    namespace App\Http\Controllers\API;

use App\Http\Controllers\FP\UserController;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class UsersController extends Controller
{
function dummy(Request $request){
return "test";
}
}

最佳答案

将 postman 中的 Accept header 添加到 application/json。 enter image description here

关于php - Laravel POST请求错误405 : MethodNotAllowedHttpException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42010453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com