在jQuery ajax成功回调之外调用函数?

时间:2018-03-07 20:56:51

标签: javascript jquery callback this bind

如何在jQuery AJAX之外调用函数?我假设我需要绑定this,但我不知道在哪里或多少次。

mylib = {
        do_the_ajax: function(data){
                $.ajax({
                        url: "the_internet",
                        method: "DELETE",
                        data: JSON.stringify(data),
                        success: function() {
                                do_the_other_thing(); // How can I call this?
                        },  
                        error: function() {
                                console.log("YOU FAIL!");
                        }   
                }); 
        },  
        do_the_other_thing: function() {
                console.log("WHAT THE FOX SAY");
        }   
}

我试图在do_the_other_thing内拨打do_the_ajax。在do_the_ajax内,我可以致电this.do_the_other_thing(),但我不确定如何在ajax success回调中调用它或者需要什么绑定。

2 个答案:

答案 0 :(得分:1)

如果您需要在调用函数do_the_other_thing之前执行某些逻辑,然后将其作为主调用的回调传递。

var obj = {
    mylib: {
        do_the_ajax: function(data, cb){
            $.ajax({
                    url: "the_internet",
                    method: "DELETE",
                    data: JSON.stringify(data),
                    success: function() {
                        // Execute previous logic.
                        cb(); // This will call 'do_the_other_thing()'
                    },  
                    error: function() {
                        console.log("YOU FAIL!");
                    }   
            }); 
        },  
        do_the_other_thing: function() {
            console.log("WHAT THE FOX SAY");
        }
    }   
}
                                      +----- Pass the function 'do_the_other_thing()'
                                      |
                                      v
obj.mylib.do_the_ajax(data, obj.mylib.do_the_other_thing);

答案 1 :(得分:0)

你需要像你说的那样绑定引用。或者使用()=>箭头函数进行自动绑定。

这里应该是一个有用的链接:Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?

所以我不确定这是不是你的情况,但这样的事情应该会有所帮助:

function someFuntion(){
  var myLib = {
        do_the_ajax: (data)=>{
            $.ajax({
                    url: "the_internet",
                    method: "DELETE",
                    data: JSON.stringify(data),
                    success: myLib.do_the_other_thing
                    error: function() {
                            console.log("YOU FAIL!");
                    }   
            }); 
        },  
        do_the_other_thing: function(res) {
                console.log("WHAT THE FOX SAY");
        }   
  }
}

或类似的东西:

function someFuntion(){
  var myLib = {
        do_the_ajax: (data)=>{
            $.ajax({
                    url: "the_internet",
                    method: "DELETE",
                    data: JSON.stringify(data),
                    success: (res)=>{
                         myLib.do_the_other_thing(res)
                    }
                    error: function() {
                            console.log("YOU FAIL!");
                    }   
            }); 
        },  
        do_the_other_thing: function(res) {
                console.log("WHAT THE FOX SAY");
        }   
  }
}