標籤:模組 res err throw time() and error typeof define
web前端通過ajax調用後台介面的資料,當項目比較大、介面比較多時,零散的ajax調用會使項目看上去非常淩亂,而且重複調用的幾率高。
所以我推薦在工作時將ajax進行統一的封裝,將介面進行統一的管理。
首先,在頂層js中封裝ajax的基本方法。
var ajaxDataController = function () { //列印ajax錯誤記錄檔 function printLog(result, url, params, response) { console.error(‘AJAX 請求異常 - %s\n錯誤資訊:\n%c%s\n%c請求連結:%s\n%c請求參數:%c%s\n%c返回資料:%c%s‘, ‘color:red;‘, result, ‘color:#333;‘, ‘color:blue‘, url, ‘color:#333;‘, ‘color:green‘, JSON.stringify(params), ‘color:#333;‘, ‘color:#643A3A‘, response) } function dataHandle(url, params, callback, async, method) { if (!method) { throw ‘method 參數未設定‘ } if ((typeof params) === ‘function‘) { callback = params params = null } params = params || {}; params = $.extend({ date: new Date().getTime().toString() }, params); async = async == null ? true : async; var ERROR_PROCESS_MODE = 0; if (typeof (params.ERROR_PROCESS_MODE) != "undefined") { if (params.ERROR_PROCESS_MODE==1) { ERROR_PROCESS_MODE = 1; try { delete params.ERROR_PROCESS_MODE; } catch (e) { } } } $.ajax({ async: async, url: url, dataType: ‘json‘, data: params, type: method, success: function (result, textStatus, xhr) { if (result.success === false) { printLog(result, url, params, xhr.responseText) } switch (result.status) { case 200: callback(result); break; case 400: if (window.parent) { alert(result.errorMessage); } else { alert(result.errorMessage); } break; case 511: //未登入 callback(result); break; case 512: if (ERROR_PROCESS_MODE!=1) { if (window.parent) { alert(result.errorMessage); } else { alert(result.errorMessage); } } callback(result); break; case 513: callback(result); break; default: //console.log(typeof xhr.responseText); if(xhr.responseText.indexOf("<!DOCTYPE html>")!=-1){ result={status:511,errorMessage:"登入逾時,請重新登入",resultObject:null}; callback(result); break; } if (window.parent) { alert("系統出現異常,請稍候再試"); } else { alert("系統出現異常,請稍候再試"); } callback(result); break; } }, error: function (xhr, textStatus, error) { if (window.parent) { alert("系統出現異常,請稍候再試"); } else { alert("系統出現異常,請稍候再試"); } printLog(xhr.status, textStatus + ‘ - ‘ + error, url, params, xhr.responseText); } }); } return { post: function (url, params, callback, async) { dataHandle(url, params, callback, async, ‘post‘); }, put: function (url, params, callback, async) { dataHandle(url, params, callback, async, ‘put‘); }, del: function (url, params, callback, async) { dataHandle(url, params, callback, async, ‘delete‘); }, get: function (url, params, callback, async) { dataHandle(url, params, callback, async, ‘get‘); } };};
然後,在模組中加入可供調用的介面的集合。
1 var myController = { 2 _ajaxHander: ajaxDataController(), 3 _url: { 4 //所有的介面地址 5 POST_REPORT1: ‘/report/report1‘, 6 7 POST_REPORT2: ‘/report/report2‘, 8 9 POST_REPORT3: ‘/report/report3‘10 },11 12 Report1 : function(params,callback, async) {13 14 this._ajaxHander.post(this._url.POST_REPORT1, params, function(data) {15 callback(data);16 }, async);17 },18 19 Report2 : function(params,callback, async) {20 21 this._ajaxHander.post(this._url.POST_REPORT2, params, function(data) {22 callback(data);23 }, async);24 },25 26 Report3 : function(params,callback, async) {27 28 this._ajaxHander.post(this._url.POST_REPORT3, params, function(data) {29 callback(data);30 }, async);31 }32 33 }34
最後,我們就可以直接調用相應的介面了。
1 function loadReport1(params) { 2 myController.Report1(params, function (data) { 3 if (data.status == 511) { 4 5 } 6 if(data.status==200 && data.resultObject!=null){ 7 8 }else { 9 10 }11 });12 }
模組化管理ajax