jQuery访问来自PHP函数的Ajax响应

问题描述:

I have a PHP function where I pass a variable to and it returns an array containing a start date and end date.

<?php
 function dateRangeTimeFrame($var1){
  ...

  $date['startDate'] = $startDate;
  $date['endDate'] = $endDate;

  return $date;
 }
?>

I am also trying to use this PHP function in an AJAX call so I can reuse the code. I have added this to the beginning of the page:

if (isset($_POST['dateFunction'])) {
  print_r(dateRangeTimeFrame($_POST['dateFunction']));
}

My jQuery code is as follows:

$.ajax({
    url: 'includes/functions.php',
    type: 'post',
    data: { "dateFunction": theDate},
    success: function(response) { 
        console.log(response['startDate']); 
        console.log(response.startDate); 
    }
});

My issue is that I do not know how to access the response that the php function is returning.

Here is the response I am getting from the PHP function:

Array
(
  [startDate] => 2015/01/17
  [endDate] => 2015/02/16
)

How would I go about getting these 2 vars from the PHP response?

我有一个PHP函数,我将一个变量传递给它,它返回一个包含开始日期和结束日期的数组。 p>

 &lt;?php 
 function dateRangeTimeFrame($ var1){
 ... 
 
 $ date ['startDate'] = $ startDate; 
 $  date ['endDate'] = $ endDate; 
 
返回$ date; 
} 
?&gt; 
  code>  pre> 
 
 

我也在尝试使用此功能 PHP函数在AJAX调用中,所以我可以重用代码。 我已经将它添加到页面的开头: p>

  if(isset($ _ POST ['dateFunction'])){
 print_r(dateRangeTimeFrame($ _ POST ['dateFunction  '])); 
} 
  code>  pre> 
 
 

我的jQuery代码如下: p>

  $ .ajax(  {
 url:'includes / functions.php',
 type:'post',
 data:{“dateFunction”:theDate},
 success:function(response){
 console.log(response [  'startDate']); 
 console.log(response.startDate); 
} 
}); 
  code>  pre> 
 
 

我的问题是我不知道 如何访问php函数返回的响应。 p>

以下是我从PHP函数获得的响应: p>

  Array  
(
 [startDate] =&gt; 2015/01/17 
 [endDate] =&gt; 2015/02/16 
)
  code>  pre> 
 
 

如何 我会从PHP响应中获取这两个变量吗? p> div>

You need to use JSON. Your Javascript natively understands and can parse it

if (isset($_POST['dateFunction'])) {
   echo json_encode(dateRangeTimeFrame($_POST['dateFunction']));
}

And your jQuery (note I added dataType)

$.ajax({
    url: 'includes/functions.php',
    dataType: 'json',
    type: 'post',
    data: { "dateFunction": theDate},
    success: function(response) { 
        console.log(response.startDate); 
    }
});

    <?php
     function dateRangeTimeFrame($var1){
      ...

      $date['startDate'] = $startDate;
      $date['endDate'] = $endDate;

      return json_encode($date);
     }

?>

jQuery

$.ajax({
    url: 'includes/functions.php',
    type: 'post',
    data: { "dateFunction": theDate},
    dataType: "json",
    success: function(response) { 
       console.log(response.startDate); 
    }
});

<?php
function dateRangeTimeFrame($var1) {
   // ...

   $date['startDate'] = $startDate;
   $date['endDate'] = $endDate;

   echo json_encode($date);
}
?> 

Ajax

$.ajax({
   url: 'includes/functions.php',
   type: 'post',
   data: { "dateFunction": theDate },
   success: function(response) { 
      for (var i = 0; i < response.length; i++) {
         alert(response[i].startDate);
      } 
   }
});