axios无跨域设置withCredentials无效?

问题描述

代码如下,设置了withCredentials

在第一层axiosres里面查看堆栈,
发现res.headersset-cookie属性里面的JSSESIONIDPath/authcas/

而在第二以及第三层的res.headers里面发现不断被设置set-cookie属性,而且res.request._header里面发现请求根本没有携带cookie

经过检查Cookie的确不存在跨域和Path路径不对应问题,PostMan以及JavaHttpClient测试均没有问题。
本应该被携带上Cookie却没有携带上,请问问题出在哪里?谢谢

function getCaptcha(username, password) {
    const map = new Map;
    map.set('password', password)

    const axios = Axios.create({
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        },
        baseURL: 'https://xxxx.xxxx.edu.cn',
        withCredentials: true
    })
    axios
        .get('/authcas/getCaptcha?service=http://xxxxxx.action')
        .then(res => {
            const body = res.data
            const ltLabel = body.match(/<input\s+type="hidden"\s+name="lt"\s+id="lt"\s+value="([a-zA-Z0-9.\-]+)"\s+\/>/)
            if (ltLabel.length >= 2) map.set('lt', ltLabel[1])
            else throw new Error()

            const exeLabal = body.match(/<input\s+type="hidden"\s+name="execution"\s+id="execution"\s+value="(\w+)"\s+\/>/)
            if (exeLabal.length >= 2) map.set('execution', exeLabal[1])
            else throw new Error()

            Axios
                .all([
                    axios.get(`/authcas/security/evaluate?userId=${username}`),
                    axios.get('/authcas/captcha')
                ])
                .then(() => {
                    const t = Math.random()
                    Axios
                        .all([
                            axios.get(`/authcas/captcha/master?${t}`),
                            axios.get(`/authcas/captcha/sub?${t}`)
                        ])
                        .then((res) => {
                            debugger
                        })
                        .catch(err => {
                            debugger
                        })
                })
                .catch(err => {
                    debugger
                })
        })
        .catch((err) => {
            console.error(err)
        })

}
阅读 9.4k
1 个回答

后端的Access-Control-Allow-Origin看是否设置了指定的域名

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题