0

I have the following php:

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

And the following angular:

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

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

i get code 302 but no result is found and the php script is not running.

What am i doing wrong?

Update

After debugging i the core of my PHP i can see that the variable request is not send to the server.

Why isnt it?

Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

1 Answers1

1

Note I’m no PHP expert, but I did some testing relating to one other AngularJS post request few days ago, which is quite similar to this one. So my solution here is based on my experience relating to that previous PHP post data experience. Also php manual for $_SESSION was used here.

First of all I’d put your php code in a getLoggedInUser.php file like below:

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

and then make some changes to the UserController

//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");
      }); 
}]);

I hope this helps with the issue.

Some notes: AngularJS is quite new to me, and if someone has different view how this post call should be made, then feel free to comment on this solution :-) Btw, I looked on some other php post issue solutions (without AngularJS) like this one, and I’m still uncertain what’s the best way of handling this post issue.

Community
  • 1
  • 1
jyrkim
  • 2,849
  • 1
  • 24
  • 33