欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS页面获取session(通过ajax)

程序员文章站 2024-03-20 17:40:52
...

JS页面获取session(通过ajax)

JS文件:

<script type="text/javascript">
    function getUser(){
    	 alert(window.location.host);
    	 $.ajax({
//     			url:"http://localhost:8080/SecondDemo/user/getSession",
                url:"http://"+window.location.host+"/SecondDemo/user/getSession",
    			type:"GET",
    			dataType:"text",
    			success:function(data){
    				if(data=="new")
    					window.location.href="pages/login.html";
    				else
    				{
    					alert(data);
    					window.location.href="pages/user.html";
    				}    					
    			},
    			error : function(e) {
    	            alert("出错");
    	            alert(e.responseText);
    	        },
    		})
    }
</script>

后台的java controller:

@RequestMapping(value ="/getSession",method = RequestMethod.GET)
	public void getSession(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException
	{
		if(session.isNew())
		{
			response.getWriter().write("new");
			System.out.println("出错");
		}			
		else
		{		
			String phone_id=(String) session.getAttribute("phone_id");
			System.out.println(phone_id);
			response.getWriter().write(phone_id);
		}
	}