入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2017-06-07 投稿人: 浏览次数:13065

用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="登录" onclick="denglu();"/>
			<input type="button" onclick="huoqu();" value="查询当前seesion中的管理员"/>
	</body>
</html>


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

get提交



post提交


阅读更多
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像