我在Laravel 5.8中-我一直收到这个CORS问题
我尝试过
Http failure during parsing for http://localhost:8080/api/get/pdf
添加这些代码
php artisan make:middleware Cors
重新启动本地Apache 2 <?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
return $next($request)
->header(‘Access-Control-Allow-Origin’, ‘*’)
->header(‘Access-Control-Allow-Methods’, ‘GET, POST, PUT, DELETE, OPTIONS’)
->header(‘Access-Control-Allow-Headers’, ‘X-Requested-With, Content-Type, X-Token-Auth, Authorization’);
}
}
打开sudo apachectl -k restart
-添加了这1行
app/Http/Kernel.php
刷新站点,转到控制台,仍然看到相同的CORS问题
人们将如何进行进一步的调试?
答案 0 :(得分:4)
尝试将CORS中间件设置为全局中间件。
handle function
中的CORS middleware
:
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS')
->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
}
要全局添加此中间件,请转到App\Http\Kernel
,然后在$middleware
数组中添加以下行:
\App\Http\Middleware\Cors::class,
您还可以在bootstrap/app.php
header('Access-Control-Allow-Origin', '*');
header('Access-Control-Allow-Methods', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS');
header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
希望它能起作用!
答案 1 :(得分:2)
尝试使用laravel-cors软件包,该软件包可让您使用Laravel中间件配置发送跨域资源共享标头。
答案 2 :(得分:0)
在下面向您添加.htaccess(只需添加到目标站点和原始站点)
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
希望它可以节省人的时间,快乐的编码!!!