Laravel 5.2 CORS POST出错500

问题描述 投票:2回答:1

我试图在Laravel 5.2中使用laravel-cors(https://github.com/barryvdh/laravel-cors)来更新数据,但是我总是遇到相同的错误:

XMLHttpRequest无法加载http://dev.pesanlab.com/api/v1/order/cart/add。所请求的资源上没有“ Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://localhost”。响应的HTTP状态码为500。

GET请求工作正常,但POST返回错误。

return [
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['GET', 'POST', 'PUT',  'DELETE'],
    'exposedHeaders' => ['*'],
    'maxAge' => 0,
    'hosts' => [],
];

这是我在Route.php上的代码

Route::group(['prefix' => 'api/v1'], function () {
    Route::group(['middleware' => ['web', 'cors']], function () {
        Route::post('order/cart/add','OrderController@cart_add');
    });
});

您能帮我吗?

laravel
1个回答
1
投票

您的主要问题不是请求,而是给出的错误。错误:

XMLHttpRequest cannot load   http://dev.pesanlab.com/api/v1/order/cart/add. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 500.

最后具有状态码500(内部服务器错误)。从调试开始,然后尝试查找真正的错误:

在Laravel中启用调试:

config / app.php:

'debug' => env('APP_DEBUG', true),

或使用默认的php显示错误:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

或查看laravel.log

Storage/logs/laravel.log

完成此操作后,您会看到原始错误:

© www.soinside.com 2019 - 2024. All rights reserved.