Hello Guys, How are you all? Hope You all Are Fine. Today I am trying to make request But I am facing following error: CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response in JavaScript. So Here I am Explain to you all the possible solutions here.
Without wasting your time, Let’s start This Article to Solve This Error.
- How CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Error Occurs ?
- How To Solve CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Error ?
- Solution 1: Set Access-Control-Allow-Methods
- Solution 2: Use cors
- Summary
How CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Error Occurs ?
I am trying to make request But I am facing following error.
CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response
How To Solve CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Error ?
- How To Solve CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Error ?
To Solve CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Error If You are using nodejs then Just install cors with help of this command: npm install cors –save Then, add following code in your app.js or server.js:
- CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response
To Solve CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Error You Need to set Access-Control-Allow-Methods in your header just like this: res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS'); Now, Your error must be solved: app.use(cors({ 'allowedHeaders': ['sessionId', 'Content-Type'], 'exposedHeaders': ['sessionId'], 'origin': '*', 'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE', 'preflightContinue': false })); Now, Your error must be solved.
Solution 1: Set Access-Control-Allow-Methods
You Need to set Access-Control-Allow-Methods in your header just like this.
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
Now, Your error must be solved.
Solution 2: Use cors
If You are using nodejs then Just install cors with help of this command.
npm install cors --save
Then, add following code in your app.js or server.js
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');
//enables cors
app.use(cors({
'allowedHeaders': ['sessionId', 'Content-Type'],
'exposedHeaders': ['sessionId'],
'origin': '*',
'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
'preflightContinue': false
}));
require('./router/index')(app);
Now, Your error must be solved.
Summary
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read