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

170
Views
¿Por qué mi script PHP no se ejecuta cuando lo llama AJAX JQuery?

Quiero ejecutar un script PHP al hacer clic en un botón, que descarga una imagen de una URL a mi servidor, a través de AJAX JQuery.

Aquí está el código AJAX en el archivo .html:

En la sección de cabecera:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

En la sección del cuerpo:

 <button type="button" id="btntest">Test PHP</button> <script> $("#btntest").on('click',function(){ alert("start"); var data = {"image-url" : "/serverfolder/Image.png","path" : "https://example.com/Image.png"}; $.ajax({ type: 'POST', url: "test.php", data: data, success: function(resultData) { alert("success"); } }); }); </script>

El código en el archivo test.php:

 <?php $params = $_POST['data']; $path = $params['path']; $image_url = $params['image-url']; $data = file_get_contents($path); $new = $image_url; file_put_contents($new, fopen($data, 'r')); ?>

El problema: no recibo la alerta de "éxito", sino un error interno del servidor para el archivo test.php. Si el archivo test.php solo se ve así:

 <?php $params = $_POST['data']; $path = $params['path']; $image_url = $params['image-url']; ?>

Aparece la alerta de éxito, pero eso no me ayuda, porque quiero descargar el archivo desde $path al directorio del servidor $image_url. Cualquier ayuda apreciada sobre cómo hacer que esto funcione.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

puedes enviar datos en ajax así

 data:{data:data}

eso está bien o puedes enviar datos como

 data:{image_url : "/serverfolder/Image.png",path : "https://example.com/Image.png"}

en esta forma, no necesita decodificar en el archivo php y usar como tal

 $path = $_POST['path']; $image_url = $_POST['image_url'];

ESO ES TODO

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!