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

353
Views
No se puede devolver el resultado de jQuery AJAX desde una función

Tengo una función que usa jQuery AJAX para devolver datos de un archivo de datos. Sin embargo, no puedo hacer que la función devuelva el valor. ¿Algunas ideas?

 $(document).ready(function () { function getdata() { var result = 'default value'; $.ajax({ url: 'data/json/load.json', method: 'GET', dataType: 'json', success: function (response) { result = response.data; console.log('this is working and the result is: ' + result); return result; } }); return result; } var returndata = getdata(); //I'm trying to return the results from my AJAX call here: console.log(returndata) });
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

 $(document).ready(function () { async function getdata() { await $.ajax({ url: 'https://api.publicapis.org/entries', method: 'GET', dataType: 'json', success: function (response) { result = response; } }); return result } getdata().then((result) => console.log(result)); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

over 4 years ago · Santiago Trujillo Report

0

Debes familiarizarte con el funcionamiento de Promises , son muy convenientes.

Así es como podrías usarlos en tu código:

 $(document).ready(function () { async function getdata() { var result = 'default value' await $.ajax({ url: 'https://api.publicapis.org/entries', method: 'GET', dataType: 'json', success: function (response) { result = response console.log('this is working and the result is: ' + result) } }) return result; } getdata().then(function(result) { returndata = result console.log(returndata) }) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

over 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!