完整的错误信息:
从源“ http://localhost:3000 ”访问“ https://api_url ”处的 XMLHttpRequest已被 CORS 策略阻止:预检中的 Access-Control-Allow-Headers 不允许请求标头字段 x-requested-with回复。
我无法解决这个 cors 问题。
我的Vue组件代码:
test(){
axios.get("https://api_url")
.then(response => {
this.data = response.data.data;
});
}
我还创建了一个中间件并将其添加到受保护的数组$middleware
中Kernel.php
<?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');
}
}