首页 > 其他 > 详细

node接口请求数据,发生跨域错误

时间:2020-03-10 16:10:09      阅读:52      评论:0      收藏:0      [点我收藏+]

经过我在网上搜索,找到方法

设置express允许本地跨域访问

//设置允许跨域访问该服务.
app.all(*, function (req, res, next) {
  res.header(Access-Control-Allow-Origin, *);
  //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
  res.header(Access-Control-Allow-Headers, Content-Type);
  res.header(Access-Control-Allow-Methods, *);
  res.header(Content-Type, application/json;charset=utf-8);
  next();
});
————————————————
版权声明:本文为CSDN博主「ffiing」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34545192/article/details/80177362

把app修改为router,就可以允许跨域了

 

router.all(‘*‘, function (req, res, next) {
  res.header(‘Access-Control-Allow-Origin‘, ‘*‘);
  //Access-Control-Allow-Headers ,
  res.header(‘Access-Control-Allow-Headers‘, ‘Content-Type‘);
  res.header(‘Access-Control-Allow-Methods‘, ‘*‘);
  res.header(‘Content-Type‘, ‘application/json;charset=utf-8‘);
  next();
});

 

node接口请求数据,发生跨域错误

原文:https://www.cnblogs.com/zenggaozheng/p/12455663.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!