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

378
Views
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 answers
Answer question

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 Report

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 Report

0

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

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!