我在heroku中的API CORS连接有问题

时间:2019-04-23 02:15:27

标签: node.js heroku cors

我可以在本地连接到我的API,但不能使用heroku中的API。我可能在客户端控制台中收到下一个错误:

从原点“ https://murmuring-eyrie-8472278.herokuapp.com/auth/website-token”到“ https://wizardly-snyder-a0a62273.netlify.com”的XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:否'Access-Control-Allow-来源的标头出现在请求的资源上。

我的代码是这样的:

app.use( ( req, res, next ) => {    
  res.setHeader("Access-Control-Allow-Origin", "https://wizardly-snyder- 
     a0a673.netlify.com");
  res.header("Access-Control-Allow-Credentials", "true");
  res.setHeader("Access-Control-Allow-Headers", "Authorization, Access- 
     Control-Allow-Headers, Origin,X-Requested-With,Content- 
     Type,Accept,content-type,application/json");
  res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, 
     OPTIONS ")

  if ('OPTIONS' == req.method) {
    res.send(200);
  } else {
    next();
  }
})

或者我也使用了它,但是没有用

app.use(cors( {
  credentials: 'true',  
  origin: '*', 
  methods: 'GET, POST, PUT, DELETE, OPTIONS', 
  allowedHeaders: 'Authorization, Access-Control-Allow-Headers, Origin,X- 
     Requested-With,Content-Type,Accept,content-type,application/json'
} ));

0 个答案:

没有答案