我使用 Laravel 和 laravel-cors 包作为后端。我正在使用来自不同域的 axios 发出 API 请求。基本上一切正常。
然后我创建了这个新路由来通过 id 检索单个对象。当存在带有 id 的对象时,我会收到数据的正常响应。如果没有 - 我得到一个正常的 404 响应(在网络选项卡中检查),除了在控制台中我得到这个错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://... (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
此外,axios 将其确定为不是 404 错误,而是未知的“网络错误”。我无法在我的 SPA 中正确处理此响应,并且我认为这不是预期的行为。
最佳答案
问题出在 barryvdh/laravel-cors
包的中间件。在任何其他中间件解决问题之前应用它,即我必须改变它:
'api' => [
'throttle:60,1',
'bindings',
\Barryvdh\Cors\HandleCors::class,
],
对此:
'api' => [
\Barryvdh\Cors\HandleCors::class,
'throttle:60,1',
'bindings',
],
关于laravel - Laravel、barryvdh/laravel-cors 和 axios 对 404 响应的跨源请求阻止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006064/