六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 56|回复: 0

jQuery学习笔记——ajax

[复制链接]

升级  88%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
44
 楼主| 发表于 2013-1-29 11:55:45 | 显示全部楼层 |阅读模式
<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/htmlresponse.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();out.write("<h3 class=\"term\">ABDICATION</h3>");}// Process the HTTP Get requestpublic 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
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表