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

访问后台时后台明明设置了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>

猜你喜欢

转载自blog.csdn.net/qinkaiyuan94/article/details/82686862