【问题标题】:Cross-Origin Request Blocked error with Laravel, barryvdh/laravel-cors & axios on a 404 response在 404 响应中,Laravel、barryvdh/laravel-cors 和 axios 的跨域请求被阻止错误
【发布时间】:2018-08-06 22:42:14
【问题描述】:

我使用 Laravel 和 laravel-cors 包作为后端。我正在使用来自不同域的 axios 发出 API 请求。基本上一切正常。

然后我创建了这条新路由来通过 id 检索单个对象。当具有 id 的对象存在时,我会得到正常的数据响应。如果没有 - 我得到一个正常的 404 响应(在网络选项卡中检查),除了在控制台中我得到这个错误:

跨域请求被阻止:同源策略不允许读取位于 http://... 的远程资源(原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。

另外,axios 确定它不是 404 错误,而是未知的“网络错误”。我无法在我的 SPA 中正确处理此响应,我认为这不是预期的行为。

【问题讨论】:

    标签: laravel cors single-page-application axios


    【解决方案1】:

    问题出在barryvdh/laravel-cors 包的中间件上。在任何其他中间件解决问题之前应用它,即我必须更改它:

    'api' => [
        'throttle:60,1',
        'bindings',
        \Barryvdh\Cors\HandleCors::class,
    ],
    

    到这里:

    'api' => [
        \Barryvdh\Cors\HandleCors::class,
        'throttle:60,1',
        'bindings',
     ],
    

    【讨论】:

    • 没用,Laravel 8 - 2022
    猜你喜欢
    • 2014-12-21
    • 2015-07-13
    • 2019-04-17
    • 2020-08-16
    • 2022-01-05
    • 2018-06-28
    • 1970-01-01
    • 2023-04-02
    • 2015-06-08
    相关资源
    最近更新 更多