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

166
Views
La búsqueda de Javasript no funciona en el retorno de PHP cuando funciona $ .ajax

Necesito simplemente recuperar datos del servidor a través de la solicitud POST. Solo puedo editar el código front-end. Descubrí que la función jQuery $.Ajax funciona y la función vanila javasript fetch() no. Descubrí que se debe a que el código PHP no repite el mensaje que quiero leer, sino que lo devuelve así:

 <?php //some php logic return array( 'result' => 'OK', 'message' => 'data I shall receive', ); ?>

La solicitud de Ajax devuelve {result: 'OK', message: 'data I shall receive'} con el siguiente código:

 this.sendFileToServer = async (file) =>{ let data = new FormData() data.append('file', file); let uploadUrl = this.element.getAttribute('data-uploadurl'); $.ajax({ url: uploadUrl, body:data, success: function (msg){ console.log(msg); } }) }

La solicitud de búsqueda de Javascript devuelve el error 500 con el siguiente código:

 this.sendFileToServer = async (file) =>{ let data = new FormData() data.append('file', file); let uploadUrl = this.element.getAttribute('data-uploadurl'); const response = await fetch(uploadUrl, { method: 'POST', body: data, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }).then(response => { console.log(response.text()); //I tried here return diffrent data like response.json(), but it doesn't work }); }

Sé que podría usar la función Ajax cuando funcione, pero quiero que mi código esté libre de jQuery. No entiendo por qué la función de búsqueda no funciona cuando debería ser la misma solicitud. Cualquier ayuda sobre cómo hacer que funcione o una explicación de por qué no puedo trabajar sería agradable.

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!