express中跨域访问设置

express中跨域访问设置,在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();
  });

如果发现还是会报错可能是你的url地址忘记加入"http://"

猜你喜欢

转载自blog.csdn.net/thrt520asd/article/details/84206723