The value of the ‘Access-Control-Allow-Origin‘ header in the response must not be the wildcard ‘*‘ w

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

当请求的凭据模式为“包含”时,响应中的“访问控制允许来源”标头的值不得为通配符“*”。XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制。

解决办法:

1.如果不使用Cookie,在前端axios.js当中设置axios.defaults.withCredentials = false

这里因为我封装过axios,顺便附上代码

封装axios代码

import axios from "axios";
axios.defaults.withCredentials = false
const Axios = axios.create({
    // 下面两个属性,用来设置,请求失败或者超时,自动重新请求的次数和间隙时间
    retry: 2, // 请求次数
    retryInterval: 2000 // 求期间隙
});
//请求前拦截
Axios.interceptors.request.use(config => {
    return config
},
    function (error) {
        return Promise.reject(error)
    }
);
//请求后返回数据拦截
Axios.interceptors.response.use(res => {
    return res
},
    function axiosRetryInterceptor(res) {
        var config = res.config;
        //如果配置不存在或重试属性未设置,抛出promise错误
        if (!config || !config.retry) return Promise.reject(res);
        //设置一个变量记录重新请求的次数
        config.retryCount = config.retryCount || 0;
        // 检查重新请求的次数是否超过我们设定的请求次数
        if (config.retryCount >= config.retry) {
            return Promise.reject(res);
        }
        //重新请求的次数自增
        config.retryCount += 1;
        // 创建新的Promise来处理重新请求的间隙
        var back = new Promise(function (resolve) {
            console.log("接口" + config.url + "请求超时,重新请求")
            setTimeout(function () {
                resolve();
            }, config.retryInterval || 1);
        });
        //返回axios的实体,重试请求
        return back.then(function () {
            return Axios(config);
        });
    }
);
export default Axios

2.后端指定前端具体访问地址访问,不能设置为通配符"*"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coca可口

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值