我试图添加一个标题与我的访问令牌到每个API调用.它适用于所有的GET请求,但是一旦我尝试发出POST,则不会添加头.
以下是我如何添加令牌:
app.factory('api',function ($http,$cookies) { return { init: function (token) { $http.defaults.headers.common['Token'] = token || $cookies.loginTokenCookie; } }; });
从这里得到的是:
app.run(function ($cookies,$http,$location,$rootScope,api) { $rootScope.location = $location; api.init(); });
我试过这样做,如下所示:
app.factory('api',$cookies) { return { init: function (token) { $http.defaults.headers.common['Token'] = token || $cookies.loginTokenCookie; $http.defaults.headers.post['Token'] = token || $cookies.loginTokenCookie; } }; });
$http.defaults.headers.post['Token-Post'] = token || $cookies.loginTokenCookie;