用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提交