How to recover php parameters passed by the $http({ ... data: ...?

Asked

Viewed 118 times

1

I have my controller that requests 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 don’t know how to recover the login and password passed by the controller.

1 answer

2


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

alert(JSON.stringify(response.data));
  • Thank you very much, but I didn’t express myself right. My problem is that with the GET method I wasn’t able to do the variable $postdata = file_get_contents("php://input"); receive login values and password. But I switched to POST and started receiving.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.