Ajax is often used for front-end interaction, although jQuery's crazy ajax processing function $. post (), $. get (); $. ajax () has greatly simplified ajax function processing, but we can keep improving the following is an encapsulated processing function, which is also very convenient to use:
File Name: jQuery. ajaxRequest. js.
Usage:
The Code is 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 prompt.
If (d. notification ){
If (d. type = 'OK '){
Alert ('submitted successfully ');
} Else if (d. type = 'error '){
Alert ('submission failed ');
} Else {
Alert ('error ');
}
}
}
}
The ajax file code:
The Code is as follows:
// Release 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 while reading the data... ', {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: 'hup', theme: 'green', 'LIFE': 100 });
Return;
}
} Else {
Lg ('no feedback ');
JQuery. jGrowl ('the server does not return information. ', {header: 'hup', theme: 'blue '});
Return;
}
}
JQuery. jGrowl (data, {header: 'hup', theme: 'blue '});
},
Error: function (e ){
Var s = e. status;
Switch (s ){
Case 404:
JQuery. jGrowl ('the requested page cannot be found. Please contact the system administrator. ', {header: 'hashed', theme: 'blue '});
Break;
Case 500:
JQuery. jGrowl ('server error [500]. Contact the system administrator. ', {header:' hint ', theme: 'blue '});
Break;
Default:
// Log ('unknow error ');
Lg (s );
JQuery. jGrowl ('unknown error. Contact the system administrator. ', {header:' hint ', theme: 'blue '});
}
}
});
}
Note: jQuery. jGrowl is a plug-in prompted by jquery's pop-up window. It is easy to use to automatically disappear after the prompt is completed. You will know it by google. In addition, as for the usage method, the returned data is a format agreed by our company for ease of use.