diff --git a/component/pear/module/common.js b/component/pear/module/common.js index a8ae02d23f2f2f40f55527d4897d6811eb16a56c..ed7370ea1a419f34a8870ebe615f85f0dd03567f 100644 --- a/component/pear/module/common.js +++ b/component/pear/module/common.js @@ -41,51 +41,76 @@ layui.define(['jquery', 'element','table'], function(exports) { /** - * 提交 json 数据 - * @param data 提交数据 - * @param href 提交接口 - * @param ajaxtype 提交方式 - * @param table 刷新父级表 - * @param callback 自定义回调函数 + * 提交 json 数据 + * @param href 必选 提交接口 + * @param data 可选 提交数据 + * @param ajaxtype 可选 提交方式(默认为get) + * @param table 可选 刷新父级表 + * @param callback 可选 自定义回调函数 + * @param dataType 可选 返回数据类型 智能猜测(可以是xml, json, script, 或 html) + * @param is_async 可选 请求是否异步处理。默认是 true + * @param is_cache 可选 浏览器是否缓存被请求页面。默认是 true * */ - this.submit = function(href,data,ajaxtype,table,callback){ - if(ajaxtype==''){ ajaxtype='post';} + this.submit = function(href,data,ajaxtype,table,callback,dataType,is_async,is_cache){ + if(ajaxtype=='' || ajaxtype==undefined){ ajaxtype='get';} + + if(data!==undefined){ + $.ajaxSetup({data:JSON.stringify(data)}); + }else { + $.ajaxSetup({data:''}); + } + if(is_cache!==undefined){ + $.ajaxSetup({dataType:dataType }); + } + if(is_async!==undefined){ + $.ajaxSetup({async:is_async }); + } + if(is_cache!==undefined){ + $.ajaxSetup({cache:is_cache }); + } $.ajax({ url:href, - data:JSON.stringify(data), - dataType:'json', contentType:'application/json', type:ajaxtype, success:callback !=null?callback:function(result){ if(result.code==1){ layer.msg(result.msg,{icon:1,time:1000},function(){ - parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页 - if(table!=null){parent.layui.table.reload(table);} + if(parent.layer.getFrameIndex(window.name)!=undefined){ + parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页 + if(table!=null){parent.layui.table.reload(table);} + }else { + if(table!=null){layui.table.reload(table);} + } }); }else{ layer.msg(result.msg,{icon:2,time:1000}); } }, - //异步访问出错的时候会用到error,可以扩充 - error:function(XMLHttpRequest){ - if(XMLHttpRequest.status==419) + error:function(xhr){ + if(xhr.status==401) + { + layer.msg('权限不足,您无法访问受限资源或数据',{icon: 5}); + } + if(xhr.status==404) + { + layer.msg('请求url地址错误,请确认后刷新重试',{icon: 5}); + } + if(xhr.status==419) { layer.msg('长时间未操作,自动刷新后重试!',{icon: 5}); setTimeout(function () { window.location.reload();}, 2000); } - if(XMLHttpRequest.status==429) + if(xhr.status==429) { layer.msg('尝试次数太多,请一分钟后再试',{icon: 5}); } - if(XMLHttpRequest.status==500) + if(xhr.status==500) { - layer.msg(XMLHttpRequest.responseJSON.message,{icon: 5}); + layer.msg(xhr.responseJSON.message,{icon: 5}); } - }, - //完成的时候会用到,例如更新token - complete:function (xhr,status){ - console.log(xhr); - console.log(status); + } + ,complete:function (xhr,status){ + } }) }