【问题标题】:Getting 500 error with Laravel 5.2 CORS POSTLaravel 5.2 CORS POST 出现 500 错误
【发布时间】:2016-05-27 13:03:56
【问题描述】:

我尝试在 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”标头。因此,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');
    });
});

你能帮帮我吗?

【问题讨论】:

  • 你能和我们分享一些代码吗?
  • 删除 web 中间件,然后重试,看看是否有效。
  • 同样的消息仍然出错
  • 将此header("Access-Control-Allow-Origin: *"); 添加到您的public.index.php 并检查它现在是否有效。

标签: laravel


【解决方案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 中启用调试:

配置/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

在你设法做到这一点后,你可以看到原来的错误:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2016-11-05
    • 2018-04-01
    • 2017-02-02
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多