zoukankan      html  css  js  c++  java
  • 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);
                });
    

      

  • 相关阅读:
    【JZOJ6223】【20190617】互膜
    【JZOJ6225】【20190618】计数
    【JZOJ6226】【20190618】纳什均衡
    【JZOJ6210】【20190612】wsm
    【学习笔记】析合树
    【JZOJ6206】【20190610】二分图边染色
    【loj3123】【CTS2019】重复
    【loj3120】【CTS2019】珍珠
    【loj3119】【CTS2019】随机立方体
    CTS&&APIO2019爆零记
  • 原文地址:https://www.cnblogs.com/sunxun/p/13188630.html
Copyright © 2011-2022 走看看