当我向我的Laravel 7.3 Passport API发出以下axios请求时:
let url = 'http://laravel.test/oauth/token'
let params = {
client_id: 4,
client_secret: 'FBkMiLI8ecdb4A8OhLRDGS1SasZP5NT7i9Qpp7bP',
grant_type: 'password',
username: 'me@home.com',
password: '1qaz@WSX',
scope: '*'
}
let headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}
axios.post(url, params, headers)
.then(response => {
this.access_token = response['data']['access_token'];
this.get_users_data()
})
.catch(response => {
// eslint-disable-next-line
console.log(response)
});
我在我的 javascript 控制台中收到此错误:
从源“ http://localhost:3000 ”访问“ http://laravel.test/oauth/token ”的 XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:无“访问” -Control-Allow-Origin' 标头存在于请求的资源上。
此外,config/cors.php
在 Laravel 7.3 中配置为允许任何内容(默认情况下):
<?php
return [
/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your settings for cross-origin resource sharing
| or "CORS". This determines what cross-origin operations may execute
| in web browsers. You are free to adjust these settings as needed.
|
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
*/
'paths' => ['api/*'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
];
我的请求有什么问题?