日期:2014-05-16  浏览次数:20583 次

jQuery学习笔记——ajax
<div class="letter" id="letter-a">
	<h3>
		<a href="#">A</a>
	</h3>
</div>
<div id="dictionary">
</div>

?

$(document).ready(function () {
	$("#letter-a a").click(function () {
		$.get("jqueryGet", {"term":$(this).attr('href')}, function (data) {
			$("#dictionary").html(data);
		});
		alert("test");
		return false;
	});
});

?

public class JqueryGet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("POST");
		System.out.println(request.getParameter("term"));
		
		// 返回值为字符串类型,相应的head设置为text/html
		response.setContentType("text/html;charset=UTF-8");

		PrintWriter out = response.getWriter();

		out.write("<h3 class=\"term\">ABDICATION</h3>");
	}

	// Process the HTTP Get request
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("GET");
		doPost(request, response);
	}

}

?

说明:

1$.get方法的第一个参数是链接后台的URL

2$.get方法的第二个参数是传递给后台的Parameter

3$(this)是点击的对象,$(this).attr(‘href’)的值是’#’$(this).text()的值是’A’

4$.get方法的第三个参数是回调函数,参数data可以是字符串,也可以是dom对象或者json对象

5$(“#dictionary”).html(data)相当于setHTML

6$(“#dictionary”).html()相当于getHTML

7alert(“test”)可能会先于回调函数执行,这就是异步调用

8return false是为了防止单击这些链接时打开新的URL,因此在事件处理程序中必须返回false

?