在 AJAX 调用后添加授权 Header

Add Authorization Header After AJAX Call

我正在使用 Angular.JS 和 Node.JS 作为 back-end 开发 Web 应用程序。 在我的登录页面上,我使用 AJAX 调用来执行登录。成功登录后,我想将浏览器重定向到新页面,但我需要根据登录响应添加 am 授权 header。

对于未来的 AJAX 调用,我使用 Angular 拦截器将令牌添加到每个请求,但我的问题是如何将其添加到重定向调用。

$http
    .post('/api/authentication/login', dataToPost)
    .success(function(data, status, headers, config){
                $window.sessionStorage.token = data.token;
                window.location.replace("/");
    })        

谢谢

您可以像这样添加headers。

$http.defaults.headers.common.Authorization = $window.sessionStorage.token