axios与express 解决跨域问题

 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
        })
    );
}

 

这都是简单操作,主要记录一个低级失误:

axios与express 解决跨域问题

 

 报错显示: 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就好了

 

axios与express 解决跨域问题

上一篇:[iOS初级教程之二]DeepLink实践


下一篇:华为AppGallery常用跳转与链接详解