【问题标题】:405 Method Not Allowed POST Laravel405 方法不允许 POST Laravel
【发布时间】:2016-08-22 13:11:06
【问题描述】:

我有一个 laravel api,我正在尝试运行自定义工匠命令来处理事务。该 api 应该检查我们的商家数据库中的待处理交易并将它们发布到我们的交易数据库中。 我收到以下错误:

[GuzzleHttp\Exception\ClientException]
  Client error: `POST http://paycentral.mymarket.com/transactions/bulk` resulted in a `405 Method Not Allowed` response:
  {"error":{"message":"405 Method Not Allowed","status_code":405,"debug":{"line":446,"file":"\/var\/www\/vhosts\/maindomai (truncated...)

我使用的 API 位于 api.mymarket.com。搜索这样的错误让我相信这是一个与 CORS 相关的问题。我正在使用 laravel-cors,并在 api.mymarket.com 和 paycentral.mymarket.com 的公共文件夹中的 .htaccess 中添加了 Header set Access-Control-Allow-Origin "*"。该错误仍然存​​在。还有其他可能的解决方法吗?我们目前正在使用 plesk 作为我们的托管服务。

更新:我尝试在支付子域中进行预检请求 来源:api.mymarket.com 访问控制请求方法:POST 访问控制请求标头:MM

它返回了 500 内部错误,我猜这是进展。

更新 这是 paycentral 的 routes.php。 cors-library 在 app.php 中注册。

paycentral routes.php

<?php

$api = app('Dingo\Api\Routing\Router');

// all routes are protected by the Authenticate middleware which makes sure     the client
// is authenticated as *somebody* - each resource is further protected by  the authorization
// policies in the App\Api\V1\Policies files to limit the method calls by which client
// type is attempting to access the resource - these must be mapped in the     AuthServiceProvider
$api->group([
    'version' => 'v1',
    'namespace' => 'App\Api\V1\Controllers',
    'middleware' => 'auth' // use the Authenticate middleware
], function($api) {

/*
     * partial CRUD resource routes
     */

    $api->get('transactions/{id}', 'TransactionController@show');
    $api->post('transactions', 'TransactionController@store');
    $api->put('transactions/{id}', 'TransactionController@update');
    $api->post('transactions/bulk', 'TransactionController@store_bulk');
    $api->post('transactions/get_updates',  'TransactionController@get_updates');

【问题讨论】:

  • 你确定route.php中的路由定义吗??
  • 您是否将 laravel-cors 库中的中间件添加到您的所有路由或特定路由?此外,它可能根本不是 CORS 问题,因为它可能会给您一个飞行前请求错误。
  • 如果你发出 curl 请求,它不应该与 CORS 有任何关系。仅当您发出跨域请求(即不同域)时才使用 CORS。
  • 它们是两个不同的子域,但我认为它们也适用。
  • 请告诉我们http://paycentral.mymarket.com/transactions/bulk的路由定义?

标签: php web-services laravel


【解决方案1】:

我解决了这个问题。这是其中一条路线未指向交易/批量的问题。之前的开发人员在没有遵循我们的版本控制方法的情况下对几个文件进行了未记录的更改,因此生产分支被破坏了。

【讨论】:

    【解决方案2】:

    假设您的路线在 routes.php 中定义良好,并且其他一切都很好。然后您可以尝试在filters.php中添加以下行

    App::before(function ($request) {
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS');
    }
    

    【讨论】:

    • 我使用的是 laravel 5.2 并且没有 filters.php。我尝试完成 onvardgmbh/laravel-filters 的步骤,但没有运气。
    • 好的,您能否在应用程序启动之前添加指定标题的行(仅用于调试),看看这是否有帮助,或者我们应该朝着不同的方向前进!
    猜你喜欢
    • 2018-06-15
    • 2014-05-23
    • 2017-06-08
    • 1970-01-01
    • 2018-12-20
    • 2015-11-16
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多