前后端分离时保持前端的cookie一直都是一个从而让后端session是同一个

用java把后端做了之后用静态页面ajax测试时发现session一直失去,一个请求就给我创建一个session的问题

主要问题在于没有传cookie过去所以session一直在变

下面是解决方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript" src="js/jquery-1.9.0.js" ></script>
		<script type="text/JavaScript">
			function denglu(){
				alert("进入单击事件");	
				var username=document.getElementById("adminUser").value;
				alert("username:"+username);
				var pass=document.getElementById("passWord").value;
				alert("password:"+pass);
				$.ajax({
					contentType:'application/json',
					xhrFields: {
           				withCredentials: true
       				},
					type:"post",
					data: JSON.stringify({
						adminUser:username,
						passWord:pass
					}),
					url:"http://localhost/api/admin/login/loginAdmin",
					success: function(data){
						alert(data);
			            alert("成功");
			            //window.location.href = 'main.html';
			   		}
				});
			}
			
			function huoqu(){
				alert("进入单击事件");	
				$.ajax({
					xhrFields: {
           				withCredentials: true
       				},
					type:"get",
					date:{},
					url:"http://localhost/api/admin/login/getSessionAdmin",
					success: function(data){
						alert(data);
			            alert("成功");
			   		}
				});
			}
		</script>
	</head>
	<body>
			用户名:<input type="text" id="adminUser" name="adminUser"/><br />
			密码:<input type="text" id="passWord" name="passWord"/><br />
			<input type="button" id="tj" value="登录" οnclick="denglu();"/>
			<input type="button" οnclick="huoqu();" value="查询当前seesion中的管理员"/>
	</body>
</html>


主要在于get、post提交时参数的问题

get提交



post提交


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值