JQuery Ajax Global Call Encapsulation Instance code detailed _jquery

Source: Internet
Author: User
Tags jquery library

There is a situation: the whole station to use asynchronous way to call the data, submit data, then you will be $.ajax each operation ({...})

Write repetitive methods and code, redundancy is too large, but also a waste of time, although you have code automatically prompt completion, but really not elegant, as a front-end geek, is not allowed!

Hey Although I do not have jquery now, but asynchronous concept is always used, to help the new people.

JQuery Ajax Universal JS Package

The first step: the introduction of the jquery Library

<script type= "Text/javascript" src= "/js/jquery.min.js" ></script>

The second step: the development of Ajax encapsulation class, has been tested through, can be directly invoked, directly affixed to the code, the explanation on the province

/***************************************************************** JQuery Ajax Package Universal Class (LINJQ) ********************** /$ (function () {/** * ajax encapsulation * URL sends the requested address * data sent to the server, array storage, such as: {"date": NE W Date (). GetTime (), "state": 1} * Async Default value: True. All requests are asynchronous requests under the default settings.
If you need to send a sync request, set this option to false.
* Note that the synchronization request will lock the browser and other actions must wait for the request to complete before it can be executed. 
* Type Request method ("POST" or "get"), default to "get" * DataType expected server returned data type, commonly used such as: XML, HTML, JSON, text * SUCCESSFN successful callback function * ERRORFN failure callback function * * jquery.ax=function (URL, data, async, type, DataType, SUCCESSFN, errorfn) {async = (Async==null | | async== "" | | typeof (async) = = "undefined")?
"True": async; Type = (Type==null | | type== "" | | | typeof (type) = = "undefined")?
"POST": type; DataType = (Datatype==null | | | datatype== "" | | typeof (DATATYPE) = "undefined")?
"JSON": dataType; data = (Data==null | | data== "" | | | typeof (DATA) = "undefined")?
{"Date": new Date (). GetTime ()}: data; $.ajax ({type:type, Async:async, Data:data, Url:url, Datatype:datatypE, success:function (d) {SUCCESSFN (d);}, Error:function (e) {ERRORFN (e);});
}; /** * Ajax Package * URL to send the requested address * data sent to the server, array storage, such as: {"date": new Date (). GetTime (), "state": 1} * SUCCESSFN successful callback function * * jQuery . axpost=function (URL, data, SUCCESSFN) {data = (Data==null | | | data== "" | | typeof (DATA) = "undefined")?
{"Date": new Date (). GetTime ()}: data;
$.ajax ({type: "Post", Data:data, Url:url, DataType: "JSON", Success:function (d) {SUCCESSFN (d);});
}; /** * Ajax Package * URL sends the requested address * data sent to the server, array storage, such as: {"date": new Date (). GetTime (), "state": 1} * DataType expected server return data type, commonly used as  : XML, HTML, JSON, text * SUCCESSFN successful callback function * ERRORFN Failure callback function * * jquery.axspost=function (URL, data, SUCCESSFN, ERRORFN) {data = (Data==null | | data== "" | | | typeof (DATA) = "undefined")?
{"Date": new Date (). GetTime ()}: data;
$.ajax ({type: "Post", Data:data, Url:url, DataType: "JSON", Success:function (d) {SUCCESSFN (d);}, Error:function (e) {
ERRORFN (e);
}
});
}; });

Step three: Invoke Impersonation

<! DOCTYPE html>  

As above code: Just fill in the URL, and the data field to be transferred on the line, to avoid duplication of work and code redundancy.

The above content is small to introduce the jquery Ajax Global Call Package instance code details of the relevant content, I hope to help!

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.