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.