[vue-cli]axios跨域问题

前后端分离,请求跨域问题。
1、已经在后端进行了跨域配置:

app.use('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://localhost:3001')
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild')
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
  res.header('Access-Control-Allow-Credentials', 'true')
  res.header('X-Powered-By', ' 3.2.1')
  res.header('Content-Type', 'application/json;charset=utf-8')
  console.log(JSON.stringify(res.header))
  if (req.method === 'OPTIONS') {
    res.send(200)
  } else {
    var _send = res.send
    var sent = false
    res.send = function (data) {
      if (sent) return
      _send.bind(res)(data)
      sent = true
    }
    next()
  }
})

但是在前端请求时还是有问题:


解决(= =):
这段代码需要写在api注册(app.use(api))之前……

你可能感兴趣的:([vue-cli]axios跨域问题)