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?

3
  • Are you using Laravel with angularJS? Commented Feb 13, 2015 at 11:13
  • @MonangShah No not right now should i? Commented Feb 13, 2015 at 11:16
  • Can you provide me link so that i can check further Commented Feb 13, 2015 at 11:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.