Ajax Data Request Instance code

Source: Internet
Author: 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 ('

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.