Simple Ajax function processing (sample code) _ jquery

Source: Internet
Author: User
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.
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.