jquery ajax UMD规范封装
(function (name, factory) { if (typeof exports === 'object') { module.exports = factory(); } else if (typeof define === 'function' && define.amd) { define(factory); } else { this[name] = factory(); } })('request', function () { //接口url前面的一般都是一样的可以提取出来 var baseUrl = 'http://132.108.200.173/imawaken/'; function ajax (type,dataType,url,data,success,error) { if(typeof data == 'function'){ error = success; success = data; data = {}; } $.ajax({ type: type, url:baseUrl+url, data: data, dataType: dataType, success: function (data) { success&&success(data); }, error:function(err){ err = err || {}; err.msg = '网络连接失败'; error&&error(err); } }) } return { //url接口地址;data:要传入的参数;success成功回调;error失败回调 post:function(url,data,success,error,async){ ajax('post','JSON',url,JSON.stringify(data),success,error,async); }, get:function(url,data,success,error,async){ ajax('get','JSON',url,data,success,error,async); }, getHTML:function(url,data,success,error,async){ ajax('get','html',url,data,success,error,async); } }; });