Axios Doesn't Send Headers, Even The CORS Preflight Already Enabled
Nodejss Server.js :
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Request-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Expose-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
next();
});
Reactjs upload :
let axiosConfig = {
headers: {
"Access-Control-Allow-Origin": "*",
'crossDomain': true,
'Content-Type': 'multipart/form-data',
'x-access-token': Cookies.get('token')
}
};
axios.post('/api/file/upload',formData, axiosConfig).then(
response => {
console.log(response);
}
).catch(
function(error){
console.log(error);
});
With postman, the post method works, when I try console.log(req.headers['x-access-token']); it returns the jwt token.
But with the browser, the post method works but the headers don't send, when I try console.log(req.headers['x-access-token']); it returns undefined. So its always give 403 status response.
node.js reactjs cors axios
add a comment |
Nodejss Server.js :
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Request-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Expose-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
next();
});
Reactjs upload :
let axiosConfig = {
headers: {
"Access-Control-Allow-Origin": "*",
'crossDomain': true,
'Content-Type': 'multipart/form-data',
'x-access-token': Cookies.get('token')
}
};
axios.post('/api/file/upload',formData, axiosConfig).then(
response => {
console.log(response);
}
).catch(
function(error){
console.log(error);
});
With postman, the post method works, when I try console.log(req.headers['x-access-token']); it returns the jwt token.
But with the browser, the post method works but the headers don't send, when I try console.log(req.headers['x-access-token']); it returns undefined. So its always give 403 status response.
node.js reactjs cors axios
The cookies are from different origin so you can't access them from current window with javascript. If you setwithCredentials
in request the browser would get/set the cross domain cookies and you could read the token cookie server side
– charlietfl
Nov 23 '18 at 2:44
AboutwithCredentials
developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/…
– charlietfl
Nov 23 '18 at 2:49
add a comment |
Nodejss Server.js :
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Request-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Expose-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
next();
});
Reactjs upload :
let axiosConfig = {
headers: {
"Access-Control-Allow-Origin": "*",
'crossDomain': true,
'Content-Type': 'multipart/form-data',
'x-access-token': Cookies.get('token')
}
};
axios.post('/api/file/upload',formData, axiosConfig).then(
response => {
console.log(response);
}
).catch(
function(error){
console.log(error);
});
With postman, the post method works, when I try console.log(req.headers['x-access-token']); it returns the jwt token.
But with the browser, the post method works but the headers don't send, when I try console.log(req.headers['x-access-token']); it returns undefined. So its always give 403 status response.
node.js reactjs cors axios
Nodejss Server.js :
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Request-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
res.header("Access-Control-Expose-Headers", "Origin, X-Requested-With, Content-Type, x-access-token");
next();
});
Reactjs upload :
let axiosConfig = {
headers: {
"Access-Control-Allow-Origin": "*",
'crossDomain': true,
'Content-Type': 'multipart/form-data',
'x-access-token': Cookies.get('token')
}
};
axios.post('/api/file/upload',formData, axiosConfig).then(
response => {
console.log(response);
}
).catch(
function(error){
console.log(error);
});
With postman, the post method works, when I try console.log(req.headers['x-access-token']); it returns the jwt token.
But with the browser, the post method works but the headers don't send, when I try console.log(req.headers['x-access-token']); it returns undefined. So its always give 403 status response.
node.js reactjs cors axios
node.js reactjs cors axios
edited Nov 23 '18 at 7:49
priyanshi srivastava
592316
592316
asked Nov 23 '18 at 2:30
Herbert Abdillah
1
1
The cookies are from different origin so you can't access them from current window with javascript. If you setwithCredentials
in request the browser would get/set the cross domain cookies and you could read the token cookie server side
– charlietfl
Nov 23 '18 at 2:44
AboutwithCredentials
developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/…
– charlietfl
Nov 23 '18 at 2:49
add a comment |
The cookies are from different origin so you can't access them from current window with javascript. If you setwithCredentials
in request the browser would get/set the cross domain cookies and you could read the token cookie server side
– charlietfl
Nov 23 '18 at 2:44
AboutwithCredentials
developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/…
– charlietfl
Nov 23 '18 at 2:49
The cookies are from different origin so you can't access them from current window with javascript. If you set
withCredentials
in request the browser would get/set the cross domain cookies and you could read the token cookie server side– charlietfl
Nov 23 '18 at 2:44
The cookies are from different origin so you can't access them from current window with javascript. If you set
withCredentials
in request the browser would get/set the cross domain cookies and you could read the token cookie server side– charlietfl
Nov 23 '18 at 2:44
About
withCredentials
developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/…– charlietfl
Nov 23 '18 at 2:49
About
withCredentials
developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/…– charlietfl
Nov 23 '18 at 2:49
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53439984%2faxios-doesnt-send-headers-even-the-cors-preflight-already-enabled%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53439984%2faxios-doesnt-send-headers-even-the-cors-preflight-already-enabled%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
The cookies are from different origin so you can't access them from current window with javascript. If you set
withCredentials
in request the browser would get/set the cross domain cookies and you could read the token cookie server side– charlietfl
Nov 23 '18 at 2:44
About
withCredentials
developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/…– charlietfl
Nov 23 '18 at 2:49