Concise Ajax function processing (sample code) _jquery

Source: Internet
Author: User

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.

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.