vue与api跨域的时候报错Access-Control-Allow-Origin
,但即使在注册possport路由的时候加上带header("Access-Control-Allow-Origin:*");
的是间件也还是会报错。
注册路由:
Passport::routes(null,['prefix'=>'api/oauth','middleware'=>'access.header']);
中间件:
public function handle($request, Closure $next)
{
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: *");
return $next($request);
}
vue:
mounted () {
axios({
method: 'post',
url: '/oauth/token',
withCredentials: true,
data: {
client_id: '2',
client_secret: 'kCvv28qy25rIw5IYvAwpIHNQvBMnH6FElrzxqvqP',
username: '[email protected]',
password: '123456',
scpoe: '',
grant_type: 'password'
}
})
.then(response => {
console.log(response.data)
})
}
在postman中测试,返回headers中是带有Access-Control-Allow-Origin -> *
的。