A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赵兵锋 中级黑马   /  2012-6-12 20:38  /  2417 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. AJAX(异步的JavaScript与XML)
  2. 缺点:不能使用浏览器的“前进”,“后退”来回退或恢复Ajax的操作。
  3. XMLHttpRequest对象,微软IE核心使用ActiveX实现,其他浏览器使用其他技术实现此对象。
  4. 使用范例:
  5.         var xmlHttpRequest = null;
  6.         if(window.activeXObject)//除undefined和null外均是true;IE浏览器里此项为真
  7.         {
  8.                 xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  9.         }else if(window.XMLHttpRequest)//除IE外的浏览器实现了此对象
  10.         {
  11.                 xmlHttpRequest = new XMLHttpRequest();
  12.         }
  13.         if(null != xmlHttpRequest)
  14.         {
  15.                 xmlHttpRequest.open("GET",url,true);//true表示以异步的方式发送请求,一般均为true。在firefox中,此项设为false后,下面的回调方法注册失效。
  16.                 xmlHttpRequest.onreadystatechange=myfun;//注册回调方法,myfun为自定义的方法。
  17.                 xmlHttpRequest.send(null);//若为POST方式,此方法可接受需提交的参数名值对字符串.如xmlHttpRequest.send("name=zhao&age=22");
  18.         }
  19.         function myfun()//一旦状态发生变化即调用此方法
  20.         {
  21.                 if(xmlHttpRequest.readyState == 4)//收到响应
  22.                 {
  23.                         if(xmlHttpRequest.status == 200)//客户端完整收到响应
  24.                         {
  25.                                 alert(xmlHttpRequest.responseText);
  26.                         }
  27.                 }
  28.         }
  29.        
  30. 若以POST方式提交,在send之前,需执行:xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

  31. jquery实现AJAX:
  32.         $.ajax({
  33.                 type:"POST",//请求类型,GET或POST
  34.                 url:"myServlet",//远程URL
  35.                 dataType:"html",//返回的数据类型,有xml、json和html(即文本)。可选,默认是html。
  36.                 data:{"param1":$("#text1").val()},//提交的参数,可选
  37.                 success:function(returnedData){//回调方法
  38.                         alert(returnedData);
  39.                 }       
  40.         });
  41.        
  42.         返回XML:
  43.                 在服务器端:
  44.                         import org.dom4j.*;
  45.                         ...//其他代码
  46.                         Document doc = DocumentHelper.createDocument();
  47.                         Element rootElement = doc.addElement("users");
  48.                         Element userElement = rootElement.addElement("user");
  49.                         Element nameElement = userElement.add("name");
  50.                         nameElement.setText("zhao");
  51.                         response.setContentType("text/xml,charset=utf-8");
  52.                         response.setHeader("pragma","no-cache");
  53.                         response.setHeader("cache-control","no-cache");
  54.                         PrintWriter out = response.getWriter();
  55.                         OutputFormat format =OutputFormat.createPrettyPrint();
  56.                         format.setEncoding("utf-8");
  57.                         XMLWriter xmlWriter = new xmlWriter(out,format);
  58.                         xmlWriter.writer(doc);
  59.                         out.flush();
  60.                 客户端:
  61.                         $.ajax({
  62.                                 type:"POST",//请求类型,GET或POST
  63.                                 url:"myServlet",//远程URL
  64.                                 dataType:"xml",//返回的数据类型,有xml、json和html(即文本)。可选,默认是html。
  65.                                 data:{"param1":$("#text1").val()},//提交的参数,可选
  66.                                 success:function(returnedData){//回调方法
  67.                                         var id = $(returnedData).find("name").text();
  68.                                 }       
  69.                         });
  70.         返回JSON:
  71.                 服务器端:
  72.                         Person p1 = new Person("zhao");
  73.                         Person p2 = new Person("li");
  74.                         ArrayList<Person> list = new ArrayList<Person>();
  75.                         list.add(p1);
  76.                         list.add(p2);       
  77.                         Gson gson = new Gson();
  78.                         String json = gson.toJson(list);
  79.                         response.setContentType("application/json;charset=utf-8");
  80.                         response.setHeader("pragma","no-cache");
  81.                         response.setHeader("cache-control","no-cache");
  82.                         PrintWriter out = response.getWriter();
  83.                         out.print(json);
  84.                         out.flush();
  85.                 客户端:
  86.                         $.get("getServlet",{},function(returnedData,status){
  87.                                 var html = "<table>";
  88.                                 for(var i=0;i<returnedData.length;i++){
  89.                                         var people returnedData[i];
  90.                                         var name = people.name;
  91.                                         html+="<tr><td>name:</td><td>"+name+"</td></tr>";
  92.                                 }
  93.                                 html+="</table>";
  94.                                 $("theBody table:eq(0)").remove();
  95.                                 $("theBody").append(html);
  96.                         });
复制代码

评分

参与人数 1技术分 +1 收起 理由
赵志勇 + 1 很给力!

查看全部评分

7 个回复

倒序浏览
总结的太好了,很实用。。
回复 使用道具 举报
谢谢,学习中!!!!
回复 使用道具 举报
学习了!
回复 使用道具 举报
非常给力,收藏了
回复 使用道具 举报
谢谢,学习中!!!!
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
111111111111111111111111111111111111111
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马