search

Home  >  Q&A  >  body text

Vuejs method ajax calls other methods on component

How to call another method in jquery ajax?

methods : {
    calert(type,msg="",error=""){
        console.log("call me");
    },
    getData(){
        $.ajax({
            type: "GET",
            success: function(data){
                // error calert not found
                calert(true,"","asd");
            },
            error: function (error) {
                // also error calert not found
                this.calert(false,"",error);
            },
            complete: function(){
            },
            url: "/test",
        });
    },
}

I tried using this.calert but it doesn't work, still error

P粉316423089P粉316423089277 days ago453

reply all(2)I'll reply

  • P粉281089485

    P粉2810894852024-02-22 11:46:37

    BTW I found the solution, it looks a bit tricky to use this

    methods : {
        calert(type,msg="",error=""){
            console.log("call me");
        },
        getData(){
            let vm = this;
            $.ajax({
                type: "GET",
                success: function(data){
                    // error calert not found
                    vm.calert(true,"","asd");
                },
                error: function (error) {
                    // also error calert not found
                    vm.calert(false,"",error);
                },
                complete: function(){
                },
                url: "/test",
            });
        },
    }

    I store this into a variable and then use that variable to call other methods.

    Does anyone have a better solution than this?

    Thanks

    reply
    0
  • P粉491421413

    P粉4914214132024-02-22 11:36:54

    You can simply update your code to use arrow functions as follows:

    methods : {
        calert(type,msg="",error=""){
            console.log("call me");
        },
        getData(){
            $.ajax({
                type: "GET",
                success: (data) => {
                    this.calert(true,"","asd");
                },
                error: (error) => {
                    this.calert(false,"",error);
                },
                complete: (){
                },
                url: "/test",
            });
        },
    }

    Alternatively, store a local reference to the method, for example:

    methods : {
        calert(type,msg="",error=""){
            console.log("call me");
        },
        getData(){
            const { calert } = this;
    
            $.ajax({
                type: "GET",
                success: function(data){
                    // error calert not found
                    calert(true,"","asd");
                },
                error: function (error) {
                    // also error calert not found
                    calert(false,"",error);
                },
                complete: function(){
                },
                url: "/test",
            });
        },
    }

    reply
    0
  • Cancelreply