1. 程式人生 > >vue axios ajax前後端分離專案localhost訪問java springboot後臺無法獲取後臺返回的cookie

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>