From c17d55ea069ecaee98928850390cbc7d7e07886b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=83=E5=BD=A9=E6=9E=AB=E5=8F=B6?= <424235748@qq.com> Date: Sun, 12 Feb 2023 14:31:42 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8=E8=BF=9E=E7=BB=AD?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=90=8C=E4=B8=80=E4=B8=AAurl=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8C=E4=B8=8D=E5=B8=A6=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E8=AF=B7=E6=B1=82=EF=BC=8C=E4=BC=9A=E5=B8=A6?= =?UTF-8?q?=E4=B8=8A=E4=B8=8A=E6=AC=A1=E5=B8=A6=E5=8F=82=E6=95=B0=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E7=9A=84=E5=8F=82=E6=95=B0=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 七彩枫叶 <424235748@qq.com> --- component/pear/module/common.js | 52 +++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/component/pear/module/common.js b/component/pear/module/common.js index a8ae02d..221affe 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){ + } }) } -- Gitee