Angular请求api和令牌

Angular request api along with token

本文关键字:令牌 api 请求 Angular      更新时间:2023-09-26

在我的angular应用中,这个函数返回的结果是:

{"error":"token_not_provided"}

函数

(function() {
    'use strict';
    angular
        .module('authApp')
        .controller('UserController', UserController);
    function UserController($http,$auth,$window) {
        var vm = this;
        vm.users;
        vm.error;
        vm.getUsers = function() {
            //console.log($window.sessionStorage.token);
            // This request will hit the index method in the AuthenticateController
            // on the Laravel side and will return the list of users
            $http.get('api/getuser').success(function(users) {
                vm.users = users;
            }).error(function(error) {
                vm.error = error;
            });
        }
    }
})();

使用facebook的身份验证是成功的,我可以获得令牌,但如果请求任何受限的api,服务器返回上述错误消息。

我的路线
Route::get('/', function () {
    return view('index');
});
Route::group(['prefix' => 'api'], function()
{
    //Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::get('getuser', 'AuthenticateController@index');
    Route::post('authenticate', 'AuthenticateController@authenticate');
});

使用邮差从受限api中获得结果,但使用浏览器给我上述错误。

谢谢。

我想你说的是JWT?试着把它添加到你的。htaccess文件中:

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]