我这里的接口数据调用的js叫interface.js,接口路径管理的js叫webSiteControl.js
/** * Created by l2776 on 2017/7/11. * 接口数据调用 * version 1.3 */ 'use strict'; define(function(require, exports, module) { "require:nomunge,exports:nomunge,module:nomunge"; var reqTimeStart = window.setTimeout(function (data) { console.log("正在加载中"+data); },1000); function CheackJSON(data) { try{ var CheckJSONParseData = JSON.parse(data); return 'application/json;charset=utf-8'; }catch(e){ return 'application/x-www-form-urlencoded'; } } function AjaxReport(data) { var newInfoRequare = {}; newInfoRequare = data; newInfoRequare.randomIf?newInfoRequare.requestData['rdm'] = Math.random():""; $.ajax({ url: newInfoRequare.interfaceName?Add.GetInterfaceSite(newInfoRequare.interfaceName):false, data: newInfoRequare.requestData, type: newInfoRequare.requestType, timeout: newInfoRequare.timout, dataType:"JSON", contentType:CheackJSON(newInfoRequare.requestData), async:true, success: function (result) { result.errorCode=='03'?require.async('./ReloginFunction.min',function (a) { a.Relogin('zewei',newInfoRequare); }):newInfoRequare.callback(result); }, error: function (xhr, status) { require.async(['./ErrFun','./InfeErrFuntion'],function (a,b) { navigator.onLine?a.Toset('网络错误,请重新请求!!'):a.Toset('接口请求有误'); b.getErrInterfaceMsg(newInfoRequare.interfaceName,xhr); }); }, complete: function (xhr, status) { window.clearTimeout(reqTimeStart); } }); } var Add = require('./webSiteControl'); exports.getDataFromServer = function(){ var InfoRequare = {}; InfoRequare.interfaceName = arguments[0] || false;//接口名称(必填),不填报错 InfoRequare.requestData = arguments[1] || {};//请求参数对象,默认为空对象 InfoRequare.callback = arguments[2]||"" ;//回调地址,会带数据一起返回,默认为空 InfoRequare.requestType = arguments[3] || "GET";//请求方式默认GET InfoRequare.randomIf = arguments[4] || false ;//接口是否需要缓存默认不需要 InfoRequare.timout = arguments[5] || 60000;//超时判定 new AjaxReport(InfoRequare); } }); 写在外部,在页面上调用即可。其中var Add = require('./webSiteControl');是调用的另一个同级webSiteControl.js,代码如下:
/** * Created by l2776 on 2017/7/11. * 接口路径管理 * version 1.0 */ 'use strict'; define(function(require, exports, module) { "require:nomunge,exports:nomunge,module:nomunge"; exports.GetInterfaceSite = function (interfaceName) { var ishttps = 'https:' == document.location.protocol ? true: false;//判定对象是否是SSL加密传输 var SiteHead = function(data){ if(data.indexOf('mg/')>=0 || data == 'BannerInfo/BusinessBannerInfo'){ return '/ypm/' }else{ return '/queryInfo/' } }; if(ishttps){ return 'https://'+window.location.host+SiteHead(interfaceName)+interfaceName; }else { return 'http://'+window.location.host+SiteHead(interfaceName)+interfaceName; } } }); 因为我常用的接口都是ypm/mg/..和queryInfo/..开头的,所以这里你需要把你自己要调用的接口换上;或者再优化整合一下 事例:
/** * Created by admin on 2017/7/11. */
define(function (require, exports, module) { var ind = require("../interface"); ind.getDataFromServer('mg/selectOrgCollections', "", getSelectInfo, "GET", true); function getSelectInfo(data) { //回调函数 }
}) 虽然封装的时候很写的很多,但是以后再用到ajax只需要调用一下即可,再也不用写那么多了,而且有利于代码优化复用
$.ajax({ type: "GET", url: "test.json", data: {}, dataType: "json", success: function () { } });