当前位置: 首页 > 知识库问答 >
问题:

CORS策略:在飞行前响应中,access-control-allog-headers不允许请求头字段x-requested-with

廖弘伟
2023-03-14

完整错误消息:

CORS策略阻止从来源“http://localhost:3000”访问位于“https://api_url”的XMLHttpRequest:飞行前响应中的access-control-allog-headers不允许请求头字段x-requested-with。

test(){
  axios.get("https://api_url")
  .then(response => {
        this.data = response.data.data;
  });
}
<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Credentials', 'true')
            ->header('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS, POST, PUT')
            ->header('Access-Control-Max-Age', '3600')
            ->header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, X-Requested-With');
    }
}

共有1个答案

逄嘉禧
2023-03-14

使用这个包,它将解决您的问题https://github.com/barryvdh/laravel-cors

 类似资料: