function func() {
    $.ajax(
    {
        type:"",
        url:"",
        data:null,
        cache:false,
        dataType:"",
        timeout:5000,
        beforeSend: function() {
            //before
        },
        success:function(e) {
            if (e.success == true) {
                //success
            }
            else {
                //error
                //dosomething 然后调用下面error那个函数
            }
        },
        error:function() {
            //error  then do something
            return false;
        }
    }
    );
}问题是就是 ,如何在success段中调用error那个匿名函数 试过将error函数构造给一个变量但是会报错。

解决方案 »

  1.   

    可以这样, 
    function doIfError(){
    //如果这里需要参数可以酌情添加
    }$.ajax(..., 
    success:function(data){
       if(){   }else{
          doIfError();
       }
    },
    error: function(){
        doIfError();
    }
    };测试通过
      

  2.   

    error:function() { 这里连函数名称都没,怎么给外部调用?
      

  3.   


    function func() { 
        $.ajax( 
        { 
            type:"", 
            url:"", 
            data:null, 
            cache:false, 
            dataType:"", 
            timeout:5000, 
            beforeSend: function() { 
                //before 
            }, 
            success:function(e) { 
                if (e.success == true) { 
                    //success 
                } 
                else { 
                    this.error();
                } 
            }, 
            error:function() { 
                alert("error");
                return false; 
            } 
        } 
        );