axios options请求后台跨域配置

时间:2022-07-30 23:16:37

app.all('*', function (req, res, next) {
         res.set('Access-Control-Allow-Origin', '*');
         res.set('Access-Control-Allow-Credentials', true);
         res.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT ,OPTIONS');
         res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization'); 
         res.header("Content-Type", "application/json;charset=utf-8");
    if (req.method.toLowerCase() == 'options')
          res.send(200); //让options尝试请求快速结束
     else
         next();
 });