const service = axios.create({
      baseURL: process.env.VUE_APP_BASE_API, // 环境变量base接口地址 url = base url + request url
      withCredentials: true, // 跨域请求时发送Cookie
      timeout: 60000, // 请求超时
      headers: {
        "Content-Type": "application/json; charset=UTF-8;"
      }
    });

node cors配置:

  //解决跨域问题
  var whitelist = ['http://example1.com', 'http://localhost:1888']
  var corsOptions = {
    origin:  function (origin, callback) {
      if (whitelist.indexOf(origin) !== -1) {
        callback(null, true)
      } else {
        callback(new Error('Not allowed by CORS'))
      }
    },
    maxAge: 5, //指定本次预检请求的有效期,单位为秒。
    credentials: true, //是否允许发送Cookie
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
  }
  app.use(cors(corsOptions))

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐