项目中访问本地node服务跨域问题

 

# 访问本地node服务跨域问题     
1 //CORS跨域接口
2     res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); // * 这个表示任意域名都可以访问,这样写不能携带cookie了 || 'http://localhost:5080'
3     res.header('Access-Control-Allow-Credentials', true); // 允许服务器端发送Cookie数据,
4                                                           // 若前端上axios.defaults.withCredentials = true设置为true了,
5                                                           // 则Access-Control-Allow-Credentials必须为true,否则会请求失败,报错
6     res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');//它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段
7     res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');  //设置方法

 

 

posted @ 2021-09-15 10:02  SlightFly  阅读(254)  评论(0编辑  收藏  举报