JQuery Ajax的小例子

helloAjax.jsp页面:

<%@ page language="java" pageEncoding="utf-8" %>
<html>
	<head>
		<title>HelloAjax</title>
		<script src="./js/jquery-1.6.1.js" type="text/javascript"></script>
		<script type="text/javascript">
			function helloAjax() {
				$.ajax({
					url: "helloAjaxDo.jsp",
					type: "post",
					data:
					{
						username: "hesmer",
						sex: "男",
						age: 25
					},
					success: function(data, textStatus) {
						$("p").append(data);
					}
				});
			};
		</script>
	</head>
	<body>
		<center>
			<button onclick="helloAjax()">Ajax</button>
			<p></p>
		</center>
	</body>
</html>
  

 helloAjaxDo.jsp页面:

<%@ page language="java" pageEncoding="utf-8" %>
<%
	request.setCharacterEncoding("utf-8");
	String username = request.getParameter("username");
	String sex = request.getParameter("sex");
	int age = Integer.parseInt(request.getParameter("age"));
	out.println("username:" + username + "<br/>");
	out.println("sex:" + sex + "<br/>");
	out.println("age:" + age + "<br/>");
%>
 

执行效果:

JQuery Ajax的小例子

相关推荐