我已经按照github仓库中自述文件中的说明安装了barryvdh / laravel-cors。我仍然没有在请求的资源错误上出现“ Access-Control-Allow-Origin”标头。我正在使用vue,axios和laravel 5.8.8
在我的api.php文件中安装了barryvdh / laravel-cors并添加了标头
我的cors.php文件看起来像这样
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedOriginsPatterns' => [],
'allowedHeaders' => ['Content-Type', 'X-Requested-With',
'Origin','Authorization'],
'allowedMethods' => ['*'],
'exposedHeaders' => [],
'maxAge' => 0,
在调试器工具中,我得到以下信息
GENERAL
Request URL:http://localhost:8000/api/entriesoff
Request Method:OPTIONS
Status Code:200 OK
Remote Address:127.0.0.1:8000
Referrer Policy:no-referrer-when-downgrade
Response Headers
Allow:GET, POST, HEAD, CONNECT, PUT, DELETE, OPTIONS, PROPFIND, MKCOL
DAV:1
Request Headers
Provisional headers are shown
Access-Control-Request-Headers:x-csrf-token,x-requested-with
Access-Control-Request-Method:GET
Origin:http://127.0.0.1:8000
Referer:http://127.0.0.1:8000/
答案 0 :(得分:0)
我找到了解决方案。我在axios URL中添加了index.php,例如http://127.0.0.1:8000/index.php/api/ ...
答案 1 :(得分:-1)
您将使用
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS'); header('Access-Control-Allow-Headers:Content-Type,Authorization,X-Requested-With');
代替 barryvdh / laravel-cors
在
public/index.php
上方
require __DIR__.'/../bootstrap/autoload.php';
示例:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
require __DIR__.'/../bootstrap/autoload.php';