Skip to content
Advertisement

Call wrapper function from inside jQuery AJAX complete function

This is what I’m attempting to do, but I’m getting an error that I can’t bind to undefined, I assume because I’m in an anonymous function. I need to access the method (getAndSayHi) the AJAX call is in.

var Parent() = new Function () {
    this.sayHi = function (name) {
        console.log("hello " + name);
    }
    this.getAndSayHi = function () {
        $.ajax({
            ....
            success: function(data) {
                this.sayHi.bind(this, data);
            }
        });
    }

How can I achieve this?

Advertisement

Answer

Try

this.getAndSayHi = function () {
    var parent = this;
    $.ajax({
        ....
        success: function(data) {
            parent.sayHi.bind(this, data);
        }
    });
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement