【问题标题】:angular $http.post returns code 302角度 $http.post 返回代码 302
【发布时间】:2015-02-13 10:43:55
【问题描述】:

我有以下 php:

    public function getLoggedInUser()
{
    if($this->isAjax())
    {
        exit (json_encode($_SESSION['User']));
    }
}

还有以下角度:

    userModule.controller('UserController', [ '$http', function($http)
{
    var userCtrl = this;
    this.user = {};

    $http.post('/User/getLoggedInUser', {request: 'ajax'}).success(function(data)
    {
       userCtrl.user = data;
    });
}]);

我得到代码 302 但没有找到结果并且 php 脚本没有运行。

我做错了什么?

更新

在调试我的 PHP 核心后,我可以看到变量 request 没有发送到服务器。

为什么不呢?

【问题讨论】:

  • 你在使用 Laravel 和 angularJS 吗?
  • @MonangShah 不,现在不应该吗?
  • 你能给我链接,以便我进一步检查

标签: angularjs angular-services


【解决方案1】:

注意我不是 PHP 专家,但几天前我做了一些与另一个 AngularJS post request 相关的测试,这与这个非常相似。所以我在这里的解决方案是基于我与以前的 PHP 发布数据经验相关的经验。这里还使用了$_SESSION 的 php 手册。

首先,我会将您的 php 代码放在 getLoggedInUser.php 文件中,如下所示:

<?php
 if ($_SERVER["REQUEST_METHOD"] === "POST")
 {
    echo $_SESSION["User"];  
 }
?>

然后对 用户控制器

//added  $scope in the controller's function parameters, just in case
userModule.controller('UserController', [ '$http', function($scope, $http)
{
    var userCtrl = this;
    this.user = {};
  //let’s pass empty data object
   var dataObj = {};
    $http.post('User/getLoggedInUser.php', dataObj).
       success(function (data, status, headers, config)
       {     
              console.log("success");

              userCtrl.user = data;
              //if the above doesn’t work, try something like below
              // $scope.userCtrl.user = data;
      })
      .error(function (data, status, headers, config)
      { 
              console.log("error");
      }); 
}]);

我希望这有助于解决这个问题。

一些注意事项:AngularJS 对我来说很新,如果有人有不同的看法应该如何进行这个 post call,那么请随时评论这个解决方案:-) 顺便说一句,我查看了其他一些 php post issue 解决方案(没有 AngularJS)就像this one,我仍然不确定处理这个帖子问题的最佳方法是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多