Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Método externo de una devolución de llamada AJAX en JavaScript y jQuery

Tengo una función en JS y jQuery que activa una llamada AJAX y tiene un bloque de callback de llamada para avisarme cuando haya terminado:

 function ajaxCall(url, type, dataType, dataToSend, callback) { if (dataType == undefined) dataType = "json"; if (dataToSend == undefined) dataToSend = null; $.ajax({ url: url, type: type, dataType: dataType, contentType: "application/json", data: dataToSend, async: true, success: function (result) { callback(result); }, error: function (data, status) { console.error("Server Error: " + status); } }); }

¡Estoy accediendo así, pero usar funciones externas como showAjaxLoader() simplemente no funciona! dice que esta función no está definida:

 function registerUser(data) { ajaxCall(pathServiceRegister, "POST", undefined, JSON.stringify(data), function (result) { // SOME CODE THAT RUNS WHEN IT'S COMPLETE // External method: showAjaxLoader(false); // Doesn't work }); }); function showAjaxLoader(show) { var loader = $('.ajax-loader'); if (show) { loader.fadeIn("fast"); } else { loader.fadeOut("fast"); } }

¿Qué estoy haciendo mal?

Gracias :)

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

¿Has intentado hacer algo como:

 var that = this; function registerUser(data) { ajaxCall(pathServiceRegister, "POST", undefined, JSON.stringify(data), function (result) { // SOME CODE THAT RUNS WHEN IT'S COMPLETE // External method: that.showAjaxLoader(false); }); });
about 4 years ago · Santiago Trujillo Report

0

Resolvió alguna muestra. esto puede ser una buena práctica. Prueba esto :

 $(document).ready(function() { $("button").click(function() {registerUser();}); }); var Scallback = function(arg) { alert("Success :"+arg); showAjaxLoader(true); } var Ecallback = function(arg) { alert("Err :"+arg); showAjaxLoader(true); } function showAjaxLoader(show) { var loader = $('.ajax-loader'); if (show) { loader.fadeIn("fast"); } else { loader.fadeOut("fast"); } } function ajaxCall(url, type, Scallback, Ecallback) { $.ajax({ url : url, type : type, async : true, success : function(result) { Scallback(result); }, error : function(data) { Ecallback(data) } }); } function registerUser() { ajaxCall(pathServiceRegister, "GET", Scallback, Ecallback); }
about 4 years ago · Santiago Trujillo Report

0

Declara tu método así

 var obj = { showAjaxLoader : function(show) { var loader = $('.ajax-loader'); if (show) { loader.fadeIn("fast"); } else { loader.fadeOut("fast"); } } }

Luego, dentro de ajax, llama a obj.showAjaxLoader(false); Esto puede funcionar.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!