axios:
// 允许跨域 axios.defaults.withCredentials=true // 允许携带cookie axios.defaults.crossDomain=true
express:
const cors = require("cors"); // 导入cors 中间件,详情看github if (process.env.NODE_ENV === "development") { //开发模式下 允许跨域访问 app.use( cors({ origin: "http://localhost:3000", credentials: true, // 是否带cookie }) ); }
这都是简单操作,主要记录一个低级失误:
报错显示: Access to XMLHttpRequest at ‘localhost:2999/user/register/getEmailRegisterAuthCode‘ from origin ‘http://localhost:3000‘ has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
查看一下axios请求的网址是不是没有带http,带上http就好了