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上传:
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);
});
在使用postman时,当我尝试console.log(req.headers ['x-access-token'])时,post方法有效。它返回jwt令牌。
但是在浏览器中,当我尝试console.log(req.headers ['x-access-token'])时,post方法有效,但标头不发送。它返回未定义。因此,它始终会给出403状态响应。