我正在尝试与nodejs服务器通信,添加授权标头时“ Preflight”失败,没有标头我就可以与服务器连接。
下添加了来自浏览器开发工具的请求标头的屏幕截图app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Authorization");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});
let params = {
"userId": userName,
"password": password,
"random": event.target.name,
"grant_type": "password"
};
var config = {
headers: {
'Authorization': 'Basic YXBwbGljYXRpb246c2VjcmV0',
'Content-Type': 'application/x-www-form-urlencoded'
}
}
[Axios.post('/oauth/token', params, config).then((response) => {
if (response.status === 200) {
if (response.data.querySuccess) {
console.log(response);
}
).catch((error) => {
console.log(error);
alert.error('Oops some error occured please check the data you have submitted');
});
}][1]