Javascript 仅在ajax返回时显示数组的一些数据

Javascript 仅在ajax返回时显示数组的一些数据,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,有没有办法只显示数组中的[原因],而不显示数据中的所有内容?有人说我需要使用JSON,但我尝试使用stringify()使用contentType:“application/JSON”,但是success:function(data)返回整个HTML而不是数组 我的问题是如何在$('#testajax').html(数据)中只显示[原因]而不是数据内部的所有内容? console.log script.js 已更新index.php console.log(日期[0]。原因) 控制台日志

有没有办法只显示数组中的[原因],而不显示数据中的所有内容?有人说我需要使用JSON,但我尝试使用stringify()使用
contentType:“application/JSON”
,但是
success:function(data)
返回整个HTML而不是数组

我的问题是如何在
$('#testajax').html(数据)中只显示[原因]而不是数据内部的所有内容?
console.log

script.js

已更新index.php


console.log(日期[0]。原因)


控制台日志(日期)

index.php

然后在ajax成功函数中,只需通过

  success: function(data) {
      console.log(data.reason); //if its comming then add it to your html.
      $('#testajax').html(data.reason);
  }

如果成功,请尝试JSON.parse(数据);然后是$('#testajax').html(数据);当I
echo json_encode($reservationAvailable)时,这是一个很好的图像
        <?php
    $date = "";
    $begin = "";
    $tijdsduur = "";
    $aantal = "";
    if (isset($_POST['date']) && isset($_POST['quantity'])) {
      if (isset($_POST['date'])) {
            print_r($_POST);
            echo "Yes, mail is set";
            $date = $_POST['date'];
            $begin = $_POST['begin'];
            $tijdsduur = $_POST['eind'];
            $aantal = $_POST['quantity'];
            $eind = $begin + $tijdsduur;
            $startTijd = "$date " . $begin;
            $eindTijd = "$date " . $eind . ":00";
            echo $date . "<br>";
            echo "$startTijd". "<br>";
            echo "$eindTijd". "<br>";
            echo $aantal. "<br>";
            $canmakereservation = "https://www.planyo.com/rest/?method=can_make_reservation&api_key=YOURKEY&resource_id=110556&start_time=$startTijd&end_time=$eindTijd&quantity=$aantal";
                    $cleancanmakereservation = preg_replace("/ /", "%20", $canmakereservation);
                    $reservationavailable = file_get_contents("$cleancanmakereservation");
                    $reservationAvailable = json_decode($reservationavailable, true);
                    echo "$cleancanmakereservation";
                    echo json_encode($reservationAvailable);
        }
        else {
            echo "No, mail is not set";
      }
      exit;
    }
    ?>
  success: function(data) {
      console.log(data.reason); //if its comming then add it to your html.
      $('#testajax').html(data.reason);
  }