【发布时间】:2020-10-02 23:27:48
【问题描述】:
我正在使用第 3 方 API,我从中收到以下错误:
从源“https://mydomain.xyz”访问“https://myapi/..”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:否“访问控制允许” -Origin' 标头存在于请求的资源上。
我想知道如何在我的应用程序中启用 CORS 策略。我的 .htaccess 文件是:
<IfModule mod_rewrite.c>
Header set Access-Control-Allow-Origin "*" // this line I added but no work
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
我正在使用 Laravel 应用程序。提前致谢。
【问题讨论】:
-
您也可以使用Cors package for laravel。它在 laravel 7 中默认使用
标签: laravel .htaccess laravel-5 cors cpanel