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

366
Views
La función de éxito y error de Ajax no llama correctamente en jquery

Utilicé el cartero para enviar los datos json a la URL especificada. Recibo la respuesta requerida que es json en el cartero. Quiero enviar este json a otro archivo, así que utilicé el método de publicación ajax. No se llama al método de publicación Ajax. Mi función no desencadena el éxito o el error aquí.

 <?php // get the HTTP method, path and body of the request $method = $_SERVER['REQUEST_METHOD']; $request = explode('/', trim($_SERVER['PATH_INFO'],'/')); $input = json_decode(file_get_contents('php://input'),true); $value1=json_encode($input); //echo print_r($value1,1); ?> <script type="text/javascript"> $(document).ready(function() { // alert("hello"); var jsondata = <?php echo $value1 ?>; $.ajax({ url: "/restapi/subscribe.php", type: "POST", data: { jsondata:jsondata }, dataType: "json", Content-Type: "application/json", success: function() { alert("data sent!"); }, error: function() { alert("There was an error. Try again please!"); } }); return false; }); </script>

Estoy enviando cualquiera de los datos json como entrada. Salida también datos json que deben publicarse en subscribe.php. En subscribe.php he escrito

 <?php $json=$_POST['jsondata']; echo $json; ?>
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Prueba con el siguiente código

 <script type="text/javascript"> $(document).ready(function() { // alert("hello"); var jsondata = <?php echo $value1 ?>; $.ajax({ url: "/restapi/subscribe.php", type: "POST", data: { jsondata:jsondata }, dataType: "json", contentType: "application/json; charset=utf-8", success: function() { alert("data sent!"); }, error: function() { alert("There was an error. Try again please!"); } }); return false; }); </script>

y avísame si funciona

about 4 years ago · Santiago Trujillo Report

0

Debe eliminar Content-Type: "application/json" de su código js y usar el siguiente código en su script php:

 header('Content-Type: application/json'); echo json_encode($data);
about 4 years ago · Santiago Trujillo Report

0

Creo que el problema es con Content-Type Intenta reemplazarlo con contentType:"application/json"

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