diff --git a/component/pear/module/common.js b/component/pear/module/common.js index 6f1eac8c4536213820980c8f3664281dd044f3ab..a8ae02d23f2f2f40f55527d4897d6811eb16a56c 100644 --- a/component/pear/module/common.js +++ b/component/pear/module/common.js @@ -41,29 +41,52 @@ layui.define(['jquery', 'element','table'], function(exports) { /** - * 提交 json 数据 + * 提交 json 数据 * @param data 提交数据 * @param href 提交接口 + * @param ajaxtype 提交方式 * @param table 刷新父级表 - * + * @param callback 自定义回调函数 * */ - this.submit = function(data,href,table,callback){ + this.submit = function(href,data,ajaxtype,table,callback){ + if(ajaxtype==''){ ajaxtype='post';} $.ajax({ url:href, data:JSON.stringify(data), dataType:'json', contentType:'application/json', - type:'post', - success:callback !=null?callback(result):function(result){ - if(result.success){ + 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));//关闭当前页 - parent.layui.table.reload(table); + if(table!=null){parent.layui.table.reload(table);} }); }else{ layer.msg(result.msg,{icon:2,time:1000}); } - } + }, + //异步访问出错的时候会用到error,可以扩充 + error:function(XMLHttpRequest){ + if(XMLHttpRequest.status==419) + { + layer.msg('长时间未操作,自动刷新后重试!',{icon: 5}); + setTimeout(function () { window.location.reload();}, 2000); + } + if(XMLHttpRequest.status==429) + { + layer.msg('尝试次数太多,请一分钟后再试',{icon: 5}); + } + if(XMLHttpRequest.status==500) + { + layer.msg(XMLHttpRequest.responseJSON.message,{icon: 5}); + } + }, + //完成的时候会用到,例如更新token + complete:function (xhr,status){ + console.log(xhr); + console.log(status); + } }) } }