jquery Ajax Global Call encapsulation

Source: Internet
Author: User
Tags json jquery library
there is a situation: the whole station to call the data asynchronously, submit the data, then you will have to $.ajax each operation ({...})
Write repetitive methods and code, redundancy is too large, but also a waste of time, although you have the code automatically prompt completion, but really not elegant, as a front-end geek, is not allowed.
Hey Although I do not need jquery now, but the asynchronous concept is always used, to help the new people "
jQuery Ajax Universal JS Package

First step: Introducing the jquery library

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

The second step: the development of the Ajax packaging class , has been tested, can be directly called, Direct code, the explanation will save

/***************************************************************** JQuery Ajax Package General Class (LINJQ) * * * * *
     /$ (function () {/** * Ajax package * URL to send the requested address * Data sent to the server, array storage, such as: {"date": new Date (). GetTime (), "state": 1} * Async default: True. By default, all requests are asynchronous requests.
     If you need to send a synchronization request, set this option to false.
     * Note that the sync request will lock the browser, and the user's 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 return 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 = (as Ync==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, Datat
            Ype: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} * succes SFN Successful callback function */jquery.axpost=function (URL, data, SUCCESSFN) {data = (Data==null | | data== "" | | typeof (DAT a) = = "undefined")?
        {"Date": new Date (). GetTime ()}: data;
            $.ajax ({type: "Post", Data:data, Url:url, DataType: "JSON",
            Success:function (d) {SUCCESSFN (d);
    }
        });
    
    }; /** * Ajax Package * URL to send the requested address * data sent to the server, array storage, such as: {"date": new Date (). GetTime (), "StatE ": 1} * DataType expected server returned data type, commonly used such as: XML, HTML, JSON, TEXT * SUCCESSFN Success callback function * ERRORFN failed callback function */Jque  Ry.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>  

$.axpost (Getrootpath () + "/test/ajax.html", NULL, function (data) {
                alert (data.data);
            });

The code above: Just fill in the URL, and the data field to be transferred is OK, avoid duplication of work and code redundancy.

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.