【发布时间】:2023-04-02 00:43:01
【问题描述】:
我想通过 api 读取一个 pdf 文件,但它给了我这个错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
我尝试 Ammar 回答后,错误消息变为
Access to fetch the resource from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
这是我的 cors.php
<?php
return [
'paths' => ['api/*'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'Access-Control-Allow-Origin'=> '*',
'Access-Control-Allow-Headers'=> 'Origin, Content-Type',
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => false,
'max_age' => false,
'supports_credentials' => false,
];
如何解决这个错误?
【问题讨论】:
-
你在内核中做了
protected $middleware = [ \Fruitcake\Cors\HandleCors::class,];吗? -
是的,这是第一个