0

I'm new to AngularJS but I've already done a lot of things. I did a $http.post, that worked like a charm.

Heres the Angular code

$scope.login = function(user, pass) {
$scope.bIsVisible = false;

var dados = {"user": user, "pass": pass};
console.log(dados);

$http.post('http://base.corretoconcursos.com.br/cadernos/index.php/manageUsers/login', dados)
.success(function(data, status, headers, config) {
  if (data == false) 
  {
    $scope.isLogged = false;
    $scope.bIsVisible = true;
  } else {
    $scope.isLogged = true;
    $scope.userData = data;
    console.log(data);
  }
})
.error(function(data, status, headers, config) {
  alert('failed');
  $scope.bIsVisible = true;
});

And the manageUsers/login function

function login()
{
    $postdata = file_get_contents("php://input");
    $data = json_decode($postdata);

    $username = $data->user;
    $password = md5($data->pass);

    $datafrombase = $this->UsersModel->getUser(array(
        'user' => $username,
        'pass' => $password
        ));

    if ($datafrombase != null) {
        $user = array(
                    'user' => $datafrombase['user'],
                    'type' => $datafrombase['type'],
                    'logged_in' => true
                    );
        $this->session->set_userdata($user);
    }

    if ($datafrombase != null)
        print_r(json_encode($datafrombase));
    else
        return false;
}

Alright. It's working. I retrieve some data from database and OK. The real problem is when I do a $http.get and simply by doing a request on database or not, it doesn't send back the data that I want, when I do the console.log(data), it shows me an entirely HTML page, in fact, the one that is displaying. I'm getting a 200 status OK, but, a HTML page is coming. Don't know why.

Heres the Angular code

$scope.setLoggedOn = function(on) {

if (on) {

    $http.get('http://base.corretoconcursos.com.br/cadernos/index.php/manageUsers/retrieveLogin')
      .success(function(data, status) {
          console.log(data);
      })
      .error(function(data, status, headers, config) {
        alert('failed');
      });
}
else
  $scope.isLogged = false;
};

And heres the PHP code function I'm retrieving.

function retrieveLogin()
{
    $user = null;

    $user = array(
        'user' => $this->session->userdata('user'),
        'type' => $this->session->userdata('type'),
        'logged_in' => true
        );

    print_r(json_encode($user));
}

I'm stuck. I've even tried doing just a 'return true'; inside the php function, return 'string'; but nothing will work. What so wrong am I doing?

2 Answers 2

1

Figured it out. If you're using CodeIgniter along sessions; and checking in the constructor if people are logged in to let them see that page or not; when you do the $http.get it will run the constructor and run that condition, and if they cannot see that page, even if you're just doing a request from that controller via AJAX, it won't let you make the request.

I thought that the $http.get would only request the function (i-e, verifyUserInDatabase();), and give the data, but turns out, it doesn't.

Thanks for the help.

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

Comments

0

$datafrombase is never set in retrieveLogin() so angular isn't going to receive anything.

4 Comments

I fixed it. Sorry, my bad. But the thing is, even if I just make a $user = 1 and do print_r($user), in angular, and console.log(data) will print my whole HTML page, not the data.
You shouldn't be using print_r to output data to angular, use echo
Already tried that. That's the problem, even if I do the same thing as the another $http.post I did, like the first function that is working perfectly, it does not work. It is always retrieving a whole HTML code page.
It get the HTTP response, its not emulating a browser. make sure you are setting your content type header to application/json

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.