diff --git a/component/pear/module/common.js b/component/pear/module/common.js index a8ae02d23f2f2f40f55527d4897d6811eb16a56c..221affef04efac966b3858256d63dde077af711d 100644 --- a/component/pear/module/common.js +++ b/component/pear/module/common.js @@ -41,51 +41,71 @@ layui.define(['jquery', 'element','table'], function(exports) { /** - * 提交 json 数据 + * 提交 json 数据 * @param data 提交数据 * @param href 提交接口 * @param ajaxtype 提交方式 * @param table 刷新父级表 * @param callback 自定义回调函数 * */ - this.submit = function(href,data,ajaxtype,table,callback){ + this.submit = function(href,data,ajaxtype,table,callback,is_async,is_cache){ if(ajaxtype==''){ ajaxtype='post';} + + if(data!==undefined){ + $.ajaxSetup({data:JSON.stringify(data)}); + }else { + $.ajaxSetup({data:''}); + } + 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){ + } }) }