【问题标题】:Trouble with PHP and Angular $http ajaxPHP 和 Angular $http ajax 的问题
【发布时间】:2016-03-10 10:36:11
【问题描述】:

因此,用户使用 datepicker 输入,其值是通过 ajax 发送到 PHP 的值,PHP 将返回包含以下 $anoArray 的对象数组。

第一个问题: ajax 只有使用 jquery 才能成功;使用 angular $http 返回"DATA WASNT AN INT" 如下所示(即使设置了许多以前发布的标头也是如此)...最终我想改用本机 angular $http 服务。我下面的内容确实返回了数据,但给我留下了第二个问题。

第二个问题:我希望在返回数据后执行各种操作,例如对对象进行排序并将某些 key:value 值设置为变量,但我似乎无法以能够支持的格式返回数据这个的。 注意*我对编程相当陌生(六个月自学),感谢您提供任何帮助。

应该返回什么:

[
{"nameF":"harry","nameL":"chicken","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"},
{"nameF":"johnny","nameL":"appleseed","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"}
]

我的角度控制器:

app.controller('scheduleController', ['$scope', '$window', function($scope, $window) {
    $scope.shifts = [];
    $scope.shiftAjax = function(){
        var x = $('#scheduleDate').val();
        $.ajax({
            method: 'post',
            url: 'getSchedule.php',
            data: {scheduleDate: x},    
        })
        .done(function(response){
$scope.shifts = response;
console.log(response);

        });
    }
}]);

控制台日志按我的预期显示数据,但我不能将其用作 javascript 对象进行迭代,如果将 .done 更改为:$scope.shifts = $scope.shifts = JSON.parse(response);,那么我只会得到一个 [object Object] 的控制台我仍然无法迭代或从中获取 key:values。

PHP 发布网址(getSchedule.php):

<?php
  $startTime = strtotime($_POST['scheduleDate']);
  include_once('../../classes/scheduler.php');
  if(is_int($startTime)){

    $var = new scheduler; //SENT TO classes/scheduler.php
    $jsonArray = $var->getSchedule($startTime); 

  }else{
    echo "DATA WASNT AN INT";
  };?>

【问题讨论】:

    标签: javascript php jquery angularjs ajax


    【解决方案1】:

    HOURS ONLINE 让我找到了这个解决方案……终于:

    控制器:

    app.controller('scheduleController', ['$scope', '$window', '$http', function($scope, $window, $http) {
        $scope.shifts = [];
        $scope.shiftAjax = function(){
            $http({
                url: 'getSchedule.php',
                method: 'POST',
                data: $.param({'scheduleDate': $scope.input}),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
            .then( function(response) {
                $scope.result = response.data;
            }, function () {
                console.log('ERROR: ANGULAR AJAX FAILED');
            });
        }
    }]);
    

    这部分的解决方案是正确设置 $.param 以及标题。

    PHP 发布网址(getSchedule.php):

    <?php
      $data = $_POST['scheduleDate'];
      $startTime = strtotime($data);
      include_once('scheduler.php');
      if(is_int($startTime)){
        $var = new scheduler;
        $jsonArray = $var->getSchedule($startTime);
      }else{
        echo " DATA WASNT AN INT";
      };
    ?>
    

    不知道为什么会这样,但是在调用strtotime() 之前将$_POST 放在一个单独的变量中是解决问题的关键。

    现在返回的是:

    [
    0:{"nameF":"harry","nameL":"chicken","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"},
        1:{"nameF":"johnny","nameL":"appleseed","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"}
    ]
    

    现在我可以访问在.then 中使用点表示法返回的对象 例如:

     $scope.result[0].start //which returns 1456790450
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 2011-08-11
      • 2016-06-05
      • 2014-11-01
      相关资源
      最近更新 更多