vue axios跨域 Request Method: OPTIONS问题

在进行跨域登录功能开发时,发现在使用axios进行POST请求时,实际请求方法变为OPTIONS。这涉及到简单跨域请求与复杂跨域请求的区别,OPTIONS请求是复杂跨域请求的预检阶段。当请求方法非GET/HEAD/POST,POST请求Content-Type不为规定类型,或设置了自定义header时,会触发OPTIONS预请求。axios的默认设置可能导致这个问题。解决办法包括正确设置Content-Type或调整axios配置。

今天做跨域登录功能遇到这个问题(后端已做跨域处理):
当跨域请求为post时候,请求的method变为了options。
在这里插入图片描述
其实跨域分为 简单跨域请求和复杂跨域请求:
简单跨域请求是不会发送options请求的
复杂跨域请求会发送一个预检请求options
复杂跨域请求要满足以下:
1、请求方法不是GET/HEAD/POST
2、POST请求的Content-Type并非application/x-www-form-urlencoded, multipart/form-data, 或text/plain
3、请求设置了自定义的header字段

参考链接

axios默认设置的['Content-Type']application/json,会引发复杂跨域请求,就是所谓的method为options的现象,此阶段为预请求 阶段,此阶段通过后才会发送正式的post请求。
其中引入qs模块是为了解析浏览器把参数当作字符串请求数据,导致请求参数无法传递到后台。
观察浏览器network发现两种方法最后的效果都是将Content-Type都变成了application/x-www-form-urlencoded
解决方法:

// request拦截器
service.interceptors.request.use(
  config => {
  	------------解决方法--------------------------------------------------
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded' 
    if (config.method === 'post') { 
      config.data = qs.stringify({
        ...config.data
      })
    }
    --------------------------------------------------------------
    if (store.getters.token) { // 设置全局参数uid,keyid
      // config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改

      if (config.method === 'post') { // 2019/4/10  by zzq
        const data = qs.parse(config.data)
        config.data = qs.stringify({
          keyid: 'keyid',
          uid: 'uid',
          ...data
        })
      }
      if (config.method === 'get') { // 2019/4/10  by zzzq
        config.params = {
          ...config.params,
          keyid: 'keyid',
          uid: 'uid'
        }
      }
    }
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)
:8081/#/user:1 Access to XMLHttpRequest at 'http://localhost:8080/user/list' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. xhr.js:175 POST http://localhost:8080/user/list net::ERR_FAILED dispatchXhrRequest @ xhr.js:175 xhrAdapter @ xhr.js:12 dispatchRequest @ dispatchRequest.js:52 Promise.then request @ Axios.js:57 Axios.<computed> @ Axios.js:77 wrap @ bind.js:9 getTableData @ User.vue:141 mounted @ User.vue:225 invokeWithErrorHandling @ vue.esm.js:1872 callHook @ vue.esm.js:4244 insert @ vue.esm.js:3167 invokeWithErrorHandling @ vue.esm.js:1872 invoker @ vue.esm.js:2193 invokeInsertHook @ vue.esm.js:6401 patch @ vue.esm.js:6620 Vue._update @ vue.esm.js:3972 updateComponent @ vue.esm.js:4090 get @ vue.esm.js:4504 run @ vue.esm.js:4579 flushSchedulerQueue @ vue.esm.js:4335 eval @ vue.esm.js:1998 flushCallbacks @ vue.esm.js:1924 Promise.then timerFunc @ vue.esm.js:1951 nextTick @ vue.esm.js:2008 queueWatcher @ vue.esm.js:4427 update @ vue.esm.js:4569 notify @ vue.esm.js:739 reactiveSetter @ vue.esm.js:1064 proxySetter @ vue.esm.js:4653 handleItemClick @ element-ui.common.js:3358 invokeWithErrorHandling @ vue.esm.js:1872 Vue.$emit @ vue.esm.js:3912 dispatch @ emitter.js:29 handleClick @ element-ui.common.js:4090 invokeWithErrorHandling @ vue.esm.js:1872 invoker @ vue.esm.js:2197 original._wrapper @ vue.esm.js:7609 createError.js:15 Uncaught (in promise) Error: Network Error at createError (createError.js:15:1) at XMLHttpRequest.handleError (xhr.js:87:1)
05-14
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值