Ajax向服务器发送请求

Ajax get方式向服务器发送请求:

 


post方式向服务器发送请求时候要添加上:

var name = document.getElementById("name").value;
XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); 
XMLHttpReq.send("name="+name);


index.jsp页面:




servlet代码片段:

	public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		response.setContentType("text/xml;charset=UTF-8");  
        response.setHeader("Cache-Control", "no-cache"); 
		String name = request.getParameter("name");
		System.out.println(name);
		 PrintWriter out = response.getWriter();
		 out.println(name);
		 out.close();
	}


你可能感兴趣的:(Ajax/jQuery)