The following is a processing function for encapsulation, which is also very convenient to use:
The name of the file is named: JQuery.ajaxRequest.js.
How to use:
Copy Code code as follows:
$ ('. _ajax '). Click (function () {
_ajax.request (this);
return false;
});
var _ajax = {
Request:function (o) {
var tform = $ (o). Parents (' form ');
_AR2 (Tform, ', ' _ajax._fb_request ');
},
_fb._request:function (d) {
D = eval (' (' +unescape (d) + ') '); Submit status Hints.
if (d.notification) {
if (D.type = = ' OK ') {
Alert (' submit successfully ');
}else if (d.type = = ' Error ') {
Alert (' Submit failed ');
}else{
Alert (' Error ');
}
}
}
}
The Ajax file code:
Copy Code code as follows:
Improved version _ar
function _ar2 (s, target, feedback, param, methods) {
var url, method;
var params = {};
if ((S instanceof jQuery) && s.attr ("tagName") = = ' Vform ') {
url = s.attr (' action ');
method = S.attr (' method ');
params = param | | $ (' Input, textarea, select ', s). Serialize ();
}else if ((S instanceof jQuery) && s.attr ("tagName") = = ' FORM ') {
url = s.attr (' action ');
method = S.attr (' method ');
params = param | | S.serialize ();
}else if (typeof (s) = = ' String ') {
URL = s; http://...
}
url = URL | | '';
Method = Method | | Methods | | ' Get ';
params = param? Param:params;
Jquery.ajax ({
Type:method,url:url,data:params,
Beforesend:function () {
Jquery.jgrowl (Please wait in ' data read ... ', {header: ' Hint ', theme: ' Blue '});
},
Success:function (data) {
data = Jquery.trim (data). Replace (/[\n\t\r]/gi, "");
if (feedback) {
Eval (feedback+ ' (\ ' +escape (data) + ') ');
Return
}else{
if (data) {
if (target) {
JQuery (target). HTML (data);
Jquery.jgrowl (' page request completed. ', {header: ' Hint ', theme: ' Green ', ' Life ': 100});
Return
}
}else{
LG (' no feedback ');
Jquery.jgrowl (' Server no return information. ', {header: ' Hint ', theme: ' Blue '});
Return
}
}
Jquery.jgrowl (data, {header: ' Hint ', theme: ' Blue '});
},
Error:function (e) {
var s = e.status;
Switch (s) {
Case 404:
Jquery.jgrowl (' The requested page cannot be found. Please contact your system administrator. ', {header: ' Hint ', theme: ' Blue '});
Break
Case 500:
Jquery.jgrowl (' server error [500]. Contact your system administrator. ', {header: ' Hint ', theme: ' Blue '};
Break
Default
Log (' Unknow error ');
LG (s);
Jquery.jgrowl (' unknown error. Please contact your system administrator. ', {header: ' Hint ', theme: ' Blue '});
}
}
});
}
Note: Jquery.jgrowl is a window of jQuery hints plugin, prompted to complete the automatic disappear, it is convenient to use, Google will know. In addition to the use of methods, the return of data is our company agreed a good format, easy to use.