最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax success and error function is not calling properly in jquery - Stack Overflow

programmeradmin1浏览0评论

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

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;
?>
Share Improve this question asked Mar 7, 2017 at 6:38 ManasaManasa 1873 silver badges16 bronze badges 1
  • maybe It's because of error. You misspell contentType. Content-Type is not valid js – disstruct Commented Mar 7, 2017 at 6:40
Add a ment  | 

4 Answers 4

Reset to default 5

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

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

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

Try to replace Content-Type: "application/json" with contentType: "application/json; charset=utf-8",

发布评论

评论列表(0)

  1. 暂无评论