0

I have being battling almost for day now about something that sound quite simple. I'm trying to pass an object using angular post ajax. I'm using PHP with codeigniter framework and I'm not getting any values pass. there have to be something wrong with the way I'm sending the object in angular because php is not getting anything. It's going to the right place because I'm getting a respond error saying "Trying to get property of non-object, line 173" line 173 is $AccessKey = $data->AccessKey;

this is my angular code

app.controller('loginController', function($scope, $http){

//$scope.formData = {};

$scope.processForm = function(){
    $http({
        method  : 'POST',
        url     : 'http://localhost:8888/employees/login',
        data    : '{"AccessKey":"[email protected]","Password":"candoa21"}'
    })
        .success(function(data){
            console.log(data);
        })
};



});

this is my php. i think here maybe I'm not using the right object name to retrieve the values.

public function login()
{
    $data = $this->input->post('data');

    //$data = '{"AccessKey":"[email protected]","Password":"candoa21"}';
    $data =  json_decode($data);

    $AccessKey = $data->AccessKey;
    $Password = $data->Password;

    $sql = "SELECT *
            FROM Employees
            WHERE Employees.AccessKey = ?
            AND Employees.Password = ?";

    $query = $this->db->query($sql, array($AccessKey, $Password));

    if($query->num_rows() > 0)
    {
        $query = json_encode($query->result());
        return $this->output
            ->set_content_type('application/json')
            ->set_output($query);
    }
    else
    {
        return 'Invalid AccessKey Or Password';
    }
}

}
3
  • You are sending a string atm, if you want to send an object, change it to: data: {user: {"AccessKey": "[email protected]", "password":"candoa21"}} and You should be able to retrieve the data in your php script with $this->input->post('user'); Commented Jun 9, 2015 at 15:17
  • I think data should be data: data : {"AccessKey":"[email protected]","Password":"candoa21"} Commented Jun 9, 2015 at 15:17
  • why you using json encode to get data?This should be $AccessKey = $this->input->post('AccessKey'); and $Password = $this->input->post('Password '); Commented Jun 9, 2015 at 16:03

3 Answers 3

3

Try this, use params instead of data & remove ' in params

 $http({
        url: 'http://localhost:8888/employees/login', 
        method: "POST",
        params: {"AccessKey":"[email protected]","Password":"candoa21"}
     })
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much it work. for those whom may encounter this problem in the future. I change the ajax type from post to get and did the same with my server side language. i also added $this-input->get('AccessKey') and did the same for password because I was no longer inspecting an object but a querystring. thanks so much ramamoorthy_villi
1

As per your sever side code you have to pass the data as show below.

$scope.processForm = function(){
    $http({
        method  : 'POST',
        url     : 'http://localhost:8888/employees/login',
        data    : { 
          data: JSON.stringify({  
             AccessKey: "[email protected]",
             Password:"candoa21"
           })
        }
    })
    .success(function(data){
       console.log(data);
    })
};

1 Comment

Yes if you want to post data json format you can use this way.
0

You receiving data wrong way because you are not posting data json format.This should be

$AccessKey = $this->input->post('AccessKey');
$Password = $this->input->post('Password ');

Or you can use this way

$data = $this->input->post();
$AccessKey = $data['AccessKey'];
$Password = $data['Password'];

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.