¿Cómo llamar a otro método dentro de 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", }); }, } he intentado usar this.calert pero no funciona, todavía hay error
Simplemente necesita actualizar su código para usar funciones de flecha, de la siguiente manera:
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", }); }, }O alternativamente, almacene una referencia local al método, como:
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", }); }, }Por cierto, encontré la solución, parece un poco complicado usar esto
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", }); }, } almaceno this en la variable y luego uso la variable para llamar a otros métodos.
¿Alguien tiene alguna solución mejor que esta?
Gracias