Answer the question
In order to leave comments, you need to log in
How to send authorization data to Angular?
Hello.
How to send authorization data?
Slim framework backend:
$app->get('/goods',authorize('user'), 'getGoods');
function login() {
if(!empty($_POST['email']) && !empty($_POST['password'])) {
if($_POST['email'] == 'admin' && $_POST['password'] == 'admin') {
$user = array("email"=>"admin", "firstName"=>"admin", "lastName"=>"admin", "role"=>"user");
$_SESSION['user'] = $user;
echo fix_json(json_encode($user));
}
else {
echo '{"error":{"text":"Неверный логин-пароль"}}';
}
}
else {
echo '{"error":{"text":"Необходимо авторизоваться. need login()"}}';
}
}
appServices.factory('Login', ['$resource',
function($resource){
return $resource('api/login', {}, {
post: {method:'POST',params:{email:'admin',password:'admin'}}
});
}]);
function LoginCtrl(Login, $location, $scope, $http) {
Login.post();
}
LoginCtrl.$inject = ['Login', '$location', '$scope', '$http'];
Answer the question
In order to leave comments, you need to log in
This is probably because Angular sends the request as application/json, not www-form-urlencoded.
Try writing something like this at the beginning of your PHP script:
if (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') === 0) {
$_POST = @json_decode(file_get_contents('php://input'), true);
if (!is_array($_POST)) $_POST = [];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question