How to retrieve parameters in php passed by $ http ({... data: ...?

1

I have my controller that requests the login:

app.factory('LoginService', function($http, $location, SessionService) {
return {
    login: function(data, scope) {
        $http({
            method: 'GET',
            url: 'http://localhost:8888/www/webservice/login.php',
            data: {'login': data.login, 'password': data.password}
        }).then(function successCallback(response) {
            alert(JSON.stringify(response));
            // this callback will be called asynchronously
            // when the response is available
        }, function errorCallback(response) {
            alert(JSON.stringify(response));
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    }
}
});

And my PHP class:

<?php header('Access-Control-Allow-Origin: *'); ?>
<?php header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept'); ?>
<?php header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT'); ?>
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$login = $request->login;
$senha = $request->password;

$user = 'root';
$password = 'root';
$db = 'CURRICULO';
$host = 'localhost';
$port = 3306;

//Open a new connection to the MySQL server
$mysqli = new mysqli($host, $user, $password, $db, $port);

//Output any connection error
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

//MySqli Select Query
$query = "SELECT * FROM Usuarios WHERE Login = '$login' AND Senha = '$senha'";
echo "$query";
$results = $mysqli->query($query);
$rows = array();

while($row = $results->fetch_array()) {
    $Id = $row["Id"];
    $Login = $row["Login"];
    $Tipo = $row["Tipo"];

    $rows[] = array('Id' => $Id, 'Login' => $Login, 'Tipo' => $Tipo);
}

//Frees the memory associated with a result
$results->free();
// close connection 
$mysqli->close();

echo json_encode($rows);
?>

But I do not know how to recover the login and password passed by the controller.

    
asked by anonymous 27.11.2016 / 19:25

1 answer

2

When you receive a response from $http , you get 5 parameters together: data , status , headers , config , statusText . What you want, or what owns the data itself, is the date. So your reception should be:

alert(JSON.stringify(response.data));
    
27.11.2016 / 21:35