Laravel如何解决跨域请求阻塞

mklgxw1f  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(291)

我想通过API读取一个pdf文件,但它给了我这个错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

在我尝试Ammar answer后,错误消息更改为

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.

这是我的博客

<?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,

];

如何解决这个错误?

jq6vz3qz

jq6vz3qz1#

我认为PDF文件在public文件夹中。所以你需要修改这一行:

'paths' => ['api/*'],

就像这样:

'paths' => ['*'],

因为公用文件夹路径的前缀不是api前缀组。
如果那不管用的话。可能laravel没有将CORS相关的HTTP头应用到文件的响应中。所以你需要配置Apache或Nginx。看看这个问题:
https://github.com/fruitcake/laravel-cors/issues/163

更新

尝试将此配置添加到您的Web服务器:

<Directory /path/to/your/public/folder/>
        Header set Access-Control-Allow-Origin "*"
</Directory>

相关问题