vue设置请求头报 Request header field xfilesize is not allowed by Access-Control-Allow-Headers

import axios from 'axios'
import Qs from 'qs'

const baseURL = 'http://test.dc.cszysoft.com:8089'
const Evaluation = 'http://test.dc.cszysoft.com:19417'

axios.defaults.baseURL = baseURL
axios.defaults.timeout = 5000;  //设置超时时间
axios.defaults.withCredentials = true;

axios.defaults.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };

const authenticationCode = sessionStorage.getItem("authenticationCode")
axios.interceptors.request.use(
  config => {
    // do something before request is sent

    config.headers['authenticationCode'] = authenticationCode
    console.log('config', config)
    return config

  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

 

一、问题:
跨域请求中包含自定义header字段时,浏览器console报错。

Request header field xfilesize is not allowed by Access-Control-Allow-Headers

二、原因:
包含自定义header字段的跨域请求,浏览器会先向服务器发送OPTIONS请求,探测该服务器是否允许自定义的跨域字段。

如果允许,则继续实际的POST/GET正常请求,否则,返回标题所示错误。


è¿éåå¾çæè¿°

 第4行为向服务器询问是否支持跨域的自定义header字段,服务器需要适当的应答。

 

Access-Control-Request-Headers

三、解决办法:

服务端需要对OPTIONS请求做出应答,应答header中包含 Access-Control-Allow-Headers,且值包含options请求中Access-Control-Request-Headers的值。

前端fetch请求如下: 

è¿éåå¾çæè¿°

 è¿éåå¾çæè¿°

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值