vue axios ajax前后端分离项目localhost访问java springboot后台无法获取后台返回的cookie

6 篇文章 0 订阅
3 篇文章 0 订阅

访问后台时后台明明设置了cookie,但是前台却接收不到,

由于localhost不是有效域名,会被chrome和ie无视掉。

https://cnodejs.org/topic/511f48eddf9e9fcc58320fa2

https://blog.csdn.net/lanria/article/details/18455213

原因是因为你访问前台项目时使用的是localhost,而你访问后台时使用的是 127.0.0.1或者Ip,后台返回的cookie的path("/"),会放到后台对应的根目录,如果你访问后台用的是127.0.0.1则会放到127.0.0.1根目录下,这样你通过localhost是接受不到的,所以前后台域名设置的要一样

 

如果不是localhost,则可以设置cookie的domain为对应的域名

https://blog.csdn.net/heatdeath/article/details/79260053

 

已经解决了

https://blog.csdn.net/heatdeath/article/details/79260053

https://blog.csdn.net/weixin_40648117/article/details/79066550

上代码:

vue:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
        <title>hello world</title>
    
    </head>
    <body>
    <div id="app">
        <h1>hello world</h1>
        用户名: <input type="text" v-model="username">
        <button @click="submit">提交</button>
    </div>
    </body>
    
    <script src="../static/js/vue.js"></script>
    <script src="../static/js/axios.min.js"></script>
    <script>
        axios.defaults.baseURL = 'http://localhost:8066'
        axios.defaults.withCredentials=true;
        new Vue({
            el: "#app",
            data() {
                return {
                    username: ''
                }
            },
            methods: {
                submit() {
                    axios.post('login', {
                        username: this.username
                    }).then(function (response) {
                        console.log(response);
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
            }
        })
    </script>
    </html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值