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

134
Views
Solicitud de Ajax realizada pero la posición del valor en la respuesta devuelta ya que la matriz sigue cambiando. Cómo puedo solucionar esto

Tengo un script que envía una serie de números a través de Ajax a un servidor web.

Los valores se utilizan para consultar una base de datos y luego se devuelven los valores correspondientes de esos números en la tabla, que luego envío a los respectivos divs en mi archivo html

vea abajo.

 function getData() { var faq_get = Array("1", "2", "3"); var faq_async_request = []; var responses = []; for (i in faq_get) { // you can push any aysnc method handler faq_async_request.push($.ajax({ url: "https://###########.com/data/data.php", // your url method: "post", // method GET or POST data: { mid: faq_get[i] }, success: function (data) { console.log("success of ajax response"); responses.push(data); } })); } $.when.apply(null, faq_async_request).done(function () { // all done console.log("all request completed"); console.log(responses); $("#3").html(responses[2]); $("#2").html(responses[1]); $("#1").html(responses[0]); }); }

GUIÓN PHP A CONTINUACIÓN

 $fig = $_POST['mid']; $sql = "SELECT * from data where id = '$fig'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo $row["scores"]; } }

mi script Ajax envía estos datos y recibe las respuestas que quería, pero tengo un pequeño problema. La posición de los valores se intercambia de vez en cuando.

por favor vea la imagen de abajo

posición de los datos de respuesta intercambiados

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debe procesar las respuestas en la devolución de llamada $.when , de modo que las procese en el orden en que se enviaron, no cuando se recibieron las respuestas.

 function getData() { var faq_get = Array("1", "2", "3"); var faq_async_request = faq_get.map(id => $.ajax({ url: "https://###########.com/data/data.php", // your url method: "post", // method GET or POST data: { mid: id } })); $.when(...faq_async_request).done(function(...responses) { // all done console.log("all request completed"); responses = responses.map(resp => resp[0]); // get the first value from each promise console.log(responses); $("#3").html(responses[2]); $("#2").html(responses[1]); $("#1").html(responses[0]); }); }

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!