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
BTW I found the solution, it looks a bit tricky to use this
I store
this
into a variable and then use that variable to call other methods.Does anyone have a better solution than this?
Thanks
You can simply update your code to use arrow functions as follows:
Alternatively, store a local reference to the method, for example: