Request header field X-CSRF-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response
up vote
2
down vote
favorite
I had a laravel project, when I run php artisan serve, there shows an error in browser:
Failed to load http://127.0.0.1:8000/api/news/get-news-list: Request header field X-CSRF-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response.
my code is like following:
axios.get(domainName+'/api/news/get-news-list').then(response=>{
news = response.data.list;
}).catch(function (error) {
console.log(error);
});
And I already add middleware like following:
Kernel.php
protected $middleware = [
AppHttpMiddlewareCors::class
];
protected $routeMiddleware = [
'cors' => AppHttpMiddlewareCors::class
];
Cors.php
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');
}
api.php
Route::prefix('news')->group(function () {
Route::get('get-news-list', 'APINewsController@getList')->middleware('cors');
});
Can anyone tell me how to fix this problem?
laravel cors
add a comment |
up vote
2
down vote
favorite
I had a laravel project, when I run php artisan serve, there shows an error in browser:
Failed to load http://127.0.0.1:8000/api/news/get-news-list: Request header field X-CSRF-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response.
my code is like following:
axios.get(domainName+'/api/news/get-news-list').then(response=>{
news = response.data.list;
}).catch(function (error) {
console.log(error);
});
And I already add middleware like following:
Kernel.php
protected $middleware = [
AppHttpMiddlewareCors::class
];
protected $routeMiddleware = [
'cors' => AppHttpMiddlewareCors::class
];
Cors.php
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');
}
api.php
Route::prefix('news')->group(function () {
Route::get('get-news-list', 'APINewsController@getList')->middleware('cors');
});
Can anyone tell me how to fix this problem?
laravel cors
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I had a laravel project, when I run php artisan serve, there shows an error in browser:
Failed to load http://127.0.0.1:8000/api/news/get-news-list: Request header field X-CSRF-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response.
my code is like following:
axios.get(domainName+'/api/news/get-news-list').then(response=>{
news = response.data.list;
}).catch(function (error) {
console.log(error);
});
And I already add middleware like following:
Kernel.php
protected $middleware = [
AppHttpMiddlewareCors::class
];
protected $routeMiddleware = [
'cors' => AppHttpMiddlewareCors::class
];
Cors.php
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');
}
api.php
Route::prefix('news')->group(function () {
Route::get('get-news-list', 'APINewsController@getList')->middleware('cors');
});
Can anyone tell me how to fix this problem?
laravel cors
I had a laravel project, when I run php artisan serve, there shows an error in browser:
Failed to load http://127.0.0.1:8000/api/news/get-news-list: Request header field X-CSRF-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response.
my code is like following:
axios.get(domainName+'/api/news/get-news-list').then(response=>{
news = response.data.list;
}).catch(function (error) {
console.log(error);
});
And I already add middleware like following:
Kernel.php
protected $middleware = [
AppHttpMiddlewareCors::class
];
protected $routeMiddleware = [
'cors' => AppHttpMiddlewareCors::class
];
Cors.php
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');
}
api.php
Route::prefix('news')->group(function () {
Route::get('get-news-list', 'APINewsController@getList')->middleware('cors');
});
Can anyone tell me how to fix this problem?
laravel cors
laravel cors
asked Aug 13 at 10:13
jimmy
2441212
2441212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Just add x-csrf-token
header to allowed list. In your case, it's in Cors.php
file, header Access-Control-Allow-Headers
.
Access-Control-Allow-Headers
controls what headers are allowed in CORS request.
I add 'Content-Type,Authorization, X-Requested-With,X-CSRF-Token' in Access-Control-Allow-Headers, then it works, thanks man!
– jimmy
Aug 14 at 6:36
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Just add x-csrf-token
header to allowed list. In your case, it's in Cors.php
file, header Access-Control-Allow-Headers
.
Access-Control-Allow-Headers
controls what headers are allowed in CORS request.
I add 'Content-Type,Authorization, X-Requested-With,X-CSRF-Token' in Access-Control-Allow-Headers, then it works, thanks man!
– jimmy
Aug 14 at 6:36
add a comment |
up vote
1
down vote
accepted
Just add x-csrf-token
header to allowed list. In your case, it's in Cors.php
file, header Access-Control-Allow-Headers
.
Access-Control-Allow-Headers
controls what headers are allowed in CORS request.
I add 'Content-Type,Authorization, X-Requested-With,X-CSRF-Token' in Access-Control-Allow-Headers, then it works, thanks man!
– jimmy
Aug 14 at 6:36
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Just add x-csrf-token
header to allowed list. In your case, it's in Cors.php
file, header Access-Control-Allow-Headers
.
Access-Control-Allow-Headers
controls what headers are allowed in CORS request.
Just add x-csrf-token
header to allowed list. In your case, it's in Cors.php
file, header Access-Control-Allow-Headers
.
Access-Control-Allow-Headers
controls what headers are allowed in CORS request.
answered Aug 13 at 10:16
Giedrius Kiršys
2,9491722
2,9491722
I add 'Content-Type,Authorization, X-Requested-With,X-CSRF-Token' in Access-Control-Allow-Headers, then it works, thanks man!
– jimmy
Aug 14 at 6:36
add a comment |
I add 'Content-Type,Authorization, X-Requested-With,X-CSRF-Token' in Access-Control-Allow-Headers, then it works, thanks man!
– jimmy
Aug 14 at 6:36
I add 'Content-Type,Authorization, X-Requested-With,X-CSRF-Token' in Access-Control-Allow-Headers, then it works, thanks man!
– jimmy
Aug 14 at 6:36
I add 'Content-Type,Authorization, X-Requested-With,X-CSRF-Token' in Access-Control-Allow-Headers, then it works, thanks man!
– jimmy
Aug 14 at 6:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51819968%2frequest-header-field-x-csrf-token-is-not-allowed-by-access-control-allow-headers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown