複用jquery的ajax調用模組

來源:互聯網
上載者:User

每次都寫一堆.ajax,真的很麻煩。因此封裝在一個模組中比較好。

第一,ajax調用出錯時,自動彈出錯誤對話方塊,對話方塊使用的artDialog。

第二,提供基本的post,get簡單調用方式。參數有限,適合就好。

第三,支援中英文。

下面是My Code,大家可以自己擴充:

(function (window, document, undefined) {"use strict";window.ajaxCall = {parameterErrorInfo: "",cancelText: "",// language should be either 'cn' or 'en'init: function (language) {window.ajaxCall.language = language;if (language === "cn") {window.ajaxCall.parameterErrorInfo = "參數數目必須為5或者6";window.ajaxCall.cancelText = "取消";} 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 except 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) {window.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) {window.ajaxCall.exe(urlPath, asyncWay, 'GET', dataValue, handler, errorHandler);}};}(window, document));

調用很簡單:

初始化用

window.ajaxCall.init("cn")

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

window.log.fillLog是ajax請求成功後的回呼函數。調用者自己實現。
最後一個參數,如果沒有傳遞,就使用預設的錯誤處理函數,否則就使用調用者自己的函數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.