我想通过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,
];
如何解决这个错误?
1条答案
按热度按时间jq6vz3qz1#
我认为PDF文件在
public
文件夹中。所以你需要修改这一行:就像这样:
因为公用文件夹路径的前缀不是
api
前缀组。如果那不管用的话。可能laravel没有将CORS相关的HTTP头应用到文件的响应中。所以你需要配置Apache或Nginx。看看这个问题:
https://github.com/fruitcake/laravel-cors/issues/163
更新
尝试将此配置添加到您的Web服务器: