ajax資料請求執行個體代碼

來源:互聯網
上載者:User

var Ly = {
 $ : function(id /* String */) {
  return document.getElementById(id);
 },
    addEvent : function(evt /* String */, act /* Function */, obj /* Object */) {
        obj = obj || this;
  evt = evt.substr(0, 2) == 'on' ? evt.substr(2, evt.length) : evt;
  
  var fun = function(event) {
            act.call(obj, obj, event);
        };
  
        if (obj.attachEvent) {
            evt = evt.length > 2 ? 'on' + evt : null;
            obj.attachEvent(evt, fun);
        }else if (obj.addEventListener) {
            evt = evt.length > 2 ? evt : null;
            obj.addEventListener(evt, fun, false);
        }
    }
};

Ly.Ajax = (function() {
 var xmlHttp,
  control,
  _this = this;
 
 (function() {
  if (window.top != window) {
   if (!window.top.Ly && !window.top.__LyError__) {
    window.top.__LyError__ = true;
    return;
   }
   control = window.top.Ly.Ajax.control();
   xmlHttp = window.top.Ly.Ajax.xmlHttp();
  } else {
   control = {
    list : [],
    lock : false
   };
   
   !xmlHttp && (xmlHttp = createXMLHttpRequest());
  }
 })();
 
 function method(m /* String */) {
  m = m || '';
  control.list[control.list.length - 1].method = m.toUpperCase() == 'GET' ? 'GET' : 'POST';
  return _this;
 }
 
 function charset(c /* String */) {
  var _charset;
  c = (c || '').toUpperCase();
  switch (c) {
   case 'GB2312':
    _charset = 'GB2312';
    break;
   case 'GBK':
    _charset = 'GBK';
    break;
   case 'GB18030':
    _charset = 'GB18030';
    break;
   case 'BIG5':
    _charset = 'BIG5';
    break;
   default :
    _charset = 'UTF-8';
  }
  control.list[control.list.length - 1].charset = _charset;
  return _this;
 }
 
 function url(u /* String */) {
  control.list[control.list.length - 1].url = u || document.URL;
  return _this;
 }
 
 function async(a /* String */) {
   control.list[control.list.length - 1].async = typeof a == 'boolean' ? a : true;
   return _this;
 }
 
 function success(fn /* Function */) {
  if (typeof fn == 'function' && fn.constructor == Function) {
   control.list[control.list.length - 1].success = fn;
  } else {
   control.list[control.list.length - 1].success = function() {};
  }
  return _this;
 }
 
 function failure(fn /* Function */) {
  if (typeof fn == 'object' && fn.constructor == Function) {
   control.list[control.list.length - 1].failure = fn;
  } else {
   control.list[control.list.length - 1].failure = function() {};
  }
  return _this;
 }
 
 function params(p /* Object */) {
  var _params;
  p = p || '';
  if (typeof p == 'string') {
   _params = p;
  } else if (typeof p == 'object') {
   for (var _p in p) {
    _params += '&' + _p + '=' + p[_p].toString().replace(/+/g, '%2B').replace(/&/g, '%26');
   }
   if (_params.length > 1) {
    _params = _params.substr(1);
   }
  }
  control.list[control.list.length - 1].params = _params;
  return _this;
 }
 
 function createXMLHttpRequest() {
  var xh;
  
  try {
    xh = new XMLHttpRequest();
  } catch(e) {
   try {
    xh = new ActiveXObject('Microsoft.XMLHTTP');
   } catch(e) {
    try {
     xh = new ActiveXObject('MSXML2.XMLHTTP');
    } catch(e) {
     alert('

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.