Reuse jquery's Ajax call module

Source: Internet
Author: User

Writing a bunch of. Ajax every time is really troublesome. Therefore, it is better to encapsulate it in a module.

First, when an error occurs in an Ajax call, the error dialog box is displayed automatically. The artdialog dialog box is used.

Second, provide basic post and get simple call methods. Limited parameters.

Third, both Chinese and English are supported.

Below is my code, which can be expanded by yourself:

(Function (window, document, undefined) {"use strict"; window. ajaxcall = {parametererrorinfo: "", canceltext: "", // language shocould be either 'cn' or 'en' init: function (language) {window. ajaxcall. language = language; If (Language = "cn") {window. ajaxcall. parametererrorinfo = "the number of parameters must be 5 or 6"; window. ajaxcall. canceltext = "cancel";} else {window. ajaxcall. parametererrorinfo = "parameters number must be 5 or 6"; window. ajaxcall. canceltext = "cancel" ;}}, // popup an error dialogdefualterrorhandler: function (jqxhr, textstatus) {$. dialog ({icon: "error", content: "ajax request got an error:" + textstatus, cancelval: window. ajaxcall. canceltext, OK: function () {This. close () ;}}) ;}, // execute. ajax function and handle t the returned data type is JSON // handler (MSG) will be callback when. ajax succeeded // errorhandler (jqxhr, textstatus) willl be callback if. ajax failedexe: function (urlpath, asyncway, method, datavalue, handler, errorhandler) {var error, request; If (arguments. length = 5) {error = Window. ajaxcall. defaulterrorhandler;} else if (arguments. length = 6) {error = errorhandler;} else {$. dialog ({icon: "error", content: window. ajaxcall. parametererrorinfo, cancelval: window. ajaxcall. canceltext, OK: function () {This. close () ;}}) ;}request =$. ajax ({URL: urlpath, async: asyncway, type: method, datatype: 'json', data: datavalue}); Request. done (handler); Request. fail (error) ;}, // post data to server using. ajax // handler (MSG) will be callback when. ajax succeeded // errorhandler (jqxhr, textstatus) willl be callback if. ajax failedpost: function (urlpath, asyncway, datavalue, handler, errorhandler) implements callback using ajaxcall.exe (urlpath, asyncway, 'post', datavalue, handler, errorhandler );}, // call web method with get to server using. ajax // handler (MSG) will be callback when. ajax succeeded // errorhandler (jqxhr, textstatus) willl be callback if. ajax failedget: function (urlpath, asyncway, datavalue, handler, errorhandler) handler (urlpath, asyncway, 'get', datavalue, handler, errorhandler) ;}};} (window, document ));

The call is simple:

Initialization

window.ajaxCall.init("cn")

window.ajaxCall.post("queryLog", true, data, window.log.fillLog);

Window. log. filllog is the callback function after the Ajax request is successful. Implemented by the caller.
If the last parameter is not passed, the default error handler function is used. Otherwise, the caller's function is used.

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.