前後端分離 跨域 sessionid保持一致

1.前端開發使用的VUE,後端使用的java,前後端分離,解決方法如下:

前端要將withCredentials設爲true

以ajax請求爲例:

[javascript] view plain copy
  1. $.ajax({  
  2.    url: a_cross_domain_url,  
  3.    // 將XHR對象的withCredentials設爲true  
  4.    xhrFields: {  
  5.       withCredentials: true  
  6.    }  
  7. });  

vue設置:


後端設置,以java爲例,其他語言類似:

[java] view plain copy
  1. httpResponse.setHeader("Access-Control-Allow-Credentials""true");  
  2. httpResponse.setHeader("Access-Control-Allow-Origin""http://192.168.199.240:8081");  
  3. httpResponse.setHeader("Access-Control-Allow-Headers""Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");  

Access-Control-Allow-Credentials 設爲true的話,Access-Control-Allow-Origin就不能設爲*了,只好改成具體的域了,這樣就可以多次請求取到的sessionid就一致了。


前端開發使用的jquery,後端使用的java,前後端分離,解決方法如下:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8" />  
  5.         <title></title>  
  6.         <script type="text/javascript" src="js/jquery-1.9.0.js" ></script>  
  7.         <script type="text/JavaScript">  
  8.             function denglu(){  
  9.                 alert("進入單擊事件");      
  10.                 var username=document.getElementById("adminUser").value;  
  11.                 alert("username:"+username);  
  12.                 var pass=document.getElementById("passWord").value;  
  13.                 alert("password:"+pass);  
  14.                 $.ajax({  
  15.                     contentType:'application/json',  
  16.                     xhrFields: {  
  17.                         withCredentials: true  
  18.                     },  
  19.                     type:"post",  
  20.                     data: JSON.stringify({  
  21.                         adminUser:username,  
  22.                         passWord:pass  
  23.                     }),  
  24.                     url:"http://localhost/api/admin/login/loginAdmin",  
  25.                     success: function(data){  
  26.                         alert(data);  
  27.                         alert("成功");  
  28.                         //window.location.href = 'main.html';  
  29.                     }  
  30.                 });  
  31.             }  
  32.               
  33.             function huoqu(){  
  34.                 alert("進入單擊事件");      
  35.                 $.ajax({  
  36.                     xhrFields: {  
  37.                         withCredentials: true  
  38.                     },  
  39.                     type:"get",  
  40.                     date:{},  
  41.                     url:"http://localhost/api/admin/login/getSessionAdmin",  
  42.                     success: function(data){  
  43.                         alert(data);  
  44.                         alert("成功");  
  45.                     }  
  46.                 });  
  47.             }  
  48.         </script>  
  49.     </head>  
  50.     <body>  
  51.             用戶名:<input type="text" id="adminUser" name="adminUser"/><br />  
  52.             密碼:<input type="text" id="passWord" name="passWord"/><br />  
  53.             <input type="button" id="tj" value="登錄" onclick="denglu();"/>  
  54.             <input type="button" onclick="huoqu();" value="查詢當前seesion中的管理員"/>  
  55.     </body>  
  56. </html>  


主要在於get、post提交時參數的問題

get提交



post提交


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章