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

168
Views
Transmitir el progreso del script PHP del lado del servidor al javascript del lado del cliente

Parece que algo ha cambiado en Ajax-land; un proceso que funcionó bien durante años para que el usuario supiera que el servidor realmente estaba analizando su trabajo ha dejado de funcionar.

Lo he destilado lo mejor que puedo a lo siguiente:

Lado del cliente:

 function start () { var jsonResponse = '', lastResponseLen = false; $.ajax({ url: getAjaxUrl(), method: 'POST', data: { REQ_TYPE: 'Misc_Action', SUB_TYPE: 'Misc_TestComms' }, xhrFields: { onprogress: function(e) { var thisResponse, response = e.currentTarget.response; if(lastResponseLen === false) { thisResponse = response; lastResponseLen = response.length; } else { thisResponse = response.substring(lastResponseLen); lastResponseLen = response.length; } jsonResponse = JSON.parse(thisResponse); document.getElementById('messages').innerHTML = 'Processed '+jsonResponse.count+' of '+jsonResponse.total; } }, success: function(text) { console.log('done!'); document.getElementById('messages').innerHTML = 'Process completed successfully'; } }); }

(tenga en cuenta que la url está correctamente formada)

Lado del servidor:

 function Misc_TestComms () { $runs = 25; $i = 0; echo json_encode(array('progress' => 0, 'count' => $i); flush(); ob_flush(); while ($i < $runs) { $i++; echo json_encode(array('progress' => (($i/$runs)*100), 'count' => $i); flush(); ob_flush(); sleep(1); } }

El lado del cliente no recibe mensajes progress hasta que se completa el script, momento en el que se activan las secciones de onprogress y de success .

¿Sugerencias?

about 4 years ago · Juan Pablo Isaza
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!