Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
Ajax success and error function is not calling properly in jquery

I have used postman to send the json data to the specifified url.I am getting the requred response that is json in postman.I want to send this json to some other file so i used ajax post method.Ajax post method is not called.My function is not triggering success or error here.

 <?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>

I am sending any of the json data as input.Output also json data that should be posted in subscribe.php.In subscribe.php i have written

<?php 
$json=$_POST['jsondata'];
echo $json;
?>
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Try with below code

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

and let me know if it work

over 4 years ago · Santiago Trujillo Denunciar

0

You should remove Content-Type: "application/json" from your js code and use next code in your php script:

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

0

I think the problem is with Content-Type Try to replace it with contentType:"application/json"

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda