我希望react应用程序能够将cookie存储在浏览器的cookie存储中。
我需要在axios的标头中发送withCredentials:true。
axios.defaults.withCredentials = true
export const login = (email, password) => {
return axios({
method: "POST",
url: "https://api.*****.**/login",
data: {
email: email,
password: password,
},
});
};
在后端,我使用ExpressJS cors中间件
const cors = require("cors");
const corsOptions = {
credentials: true,
};
app.use(cors(corsOptions));
在使用axios从react发出登录请求时,作为回应,我收到了错误:
Access to XMLHttpRequest at 'https://api.*****.**/login' from origin 'http://localhost:3000' has been blocked by CORS policy:
Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
从res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true })
删除选项中的域