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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 许王斌 中级黑马   /  2012-10-26 10:14  /  1413 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

谁学到邹老师的ajax的公共组件编写了,这块我怎么也搞不懂,跟了几遍代码也无果,望高手指点一二
尤其是后面添加的原型方法
  1. function AjaxRequest(method, url, data, callback) {
  2.     this.method = method;
  3.     this.url = url;
  4.     this.data = data;
  5.     this.callback = callback;

  6.     this.send = function () {
  7.         //1.创建异步对象
  8.         var xhr = createXmlHttp();
  9.         //2.设置请求参数
  10.         this.url = (this.data.length > 0 && this.method == "get") ? this.url + "?" + this.data : this.url;
  11.         xhr.open(this.method, this.url, true);
  12.         //3.根据请求方式 设置 响应的请求状态行
  13.         if (this.method == "get") {
  14.             xhr.setRequestHeader("If-Modified-Since", 0);
  15.         }
  16.         else {
  17.             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  18.         }
  19.         //4.设置回调函数
  20.         xhr.onreadystatechange = this.bind(this,function(){
  21.             if (xhr.readyState == 4 && xhr.status == 200) {
  22.                 //获得 服务器响应的json数组字符串
  23.                 this.callback(xhr.responseText);
  24.             }
  25.         });

  26.         //5.发送请求报文
  27.         if (this.method == "get") {
  28.             xhr.send(null);
  29.         }
  30.         else {
  31.             xhr.send(this.data);
  32.         }
  33.     };
  34. }

  35. //为AjaxRequest添加原型方法
  36. AjaxRequest.prototype = {
  37.     bind: function (object, fun) { return function () { return fun.apply(object, arguments); } }
  38. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马