fetch 请求 express RESTful API 请求跨域

跨域

描述:

前端使用fetch请求,接口使用express开箱即用生成器构建当请求接口时报错

在这里插入图片描述

解决方案:

前端设置:

//fetch设置
const respones = await fetch(LIST_URL, {
        mode: "cors",
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
        },
    });

express设置允许跨域:

app.all("*", (req, res, next)=> {  
    res.header("Access-Control-Allow-Origin", "*");  
    res.header("Access-Control-Allow-Headers", "Content-Type");  
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    next();  
}); 

猜你喜欢

转载自blog.csdn.net/h774140913/article/details/84990627