当前位置:编程学习 > JS >>

ajax数据请求实例代码

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('

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,