On success, jQuery Ajax call object method?

  • 0
    Can you somehow run jQuery Ajax from within the object and pass a method of the same object to success?

    With a simple example:

    class Popup {
        Load(url) {
            $.get(url, this.SomeMethod);
        }
        SomeMethod(result) {}
    }
    
    var P = new Popup();
    P.Load("/url");
    JavaScript Anonymous, Jun 28, 2020

  • 1 Answers
  • 0
    class Popup {

    Load(url) {
    const _this = this;
    $.ajax(url, {
    success: function (data) {
    _this.SomeMethod(data);
    }
    });
    }

    SomeMethod(result) {
    console.log(result);
    }

    }

    const P = new Popup();
    P.Load('https://qna.habr.com/q/883801#answers_list');
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!