Ajax 包装方法,包含刷新
记录一个通用方法,包含刷新
//异步ajax请求,默认POST方法 formTool.ajaxAsync = function(json,url,method,func){ url = this.contextPath + url; if(this.isNull(method)){ method = "POST"; }else{ method = method.toUpperCase(); } var xhr = $.ajax({ async: true, type: method, url: url, timeout:40000, data: json, beforeSend: function(request) { //请求之前调用的函数 layer.load(0, { shade: [0.2,'#fff'] //0.1透明度的白色背景 }); }, error: function(result) { layer.closeAll('loading'); if(result.status == 401){ layer.msg('登陆认证失败,请重新登陆!'); window.location.href = formTool.isPc == true ? formTool.pcLogin : formTool.mobileLogin; }else{ layer.msg('服务器异步请求失败!', {icon: 2, time: 2000}); } }, success: function(result) { layer.closeAll('loading'); if(typeof func == "function"){ func(result); } },complete:function (XMLHttpRequest,status) { layer.closeAll('loading'); if(status=='timeout'){ layer.msg('请求超时,请重试', {icon: 2, time: 2000}); xhr.abort()// 超时后中断请求 } } }); };
调用方法:
//测试 formTool.ajaxAsync({"sid": sid,"publishTime":pass} ,"/working/pulishYearSynchroData" ,"POST",function(result){ //回调 if (result.code == 0) { layer.msg(result.msg); search(); } else { layer.msg(result.msg, {icon: 2}); } layer.close(indexel); });