Angularjs:在http请求标头上设置授权

时间:2022-08-22 19:34:34

in Angularjs i have a login page and if a user succesfully sign in i set the Authorization attribute as default on subsequent http request header:

在Angularjs我有一个登录页面,如果用户成功登录,我将Authorization属性设置为后续http请求标头的默认值:

$http.defaults.headers.common.Authorization = 'Bearer ' + data.access_token;

So, anytime i make a request, in the header is present the Authorization parameter and all works. The problem is that if i refresh the page, in the header request isn't present the Authorization parameter and the request fail.

因此,无论何时我发出请求,都会在标题中显示Authorization参数并且所有工作都有效。问题是如果我刷新页面,在头部请求中不存在Authorization参数和请求失败。

Why isn't present the Authorization parameter if i set it as default in the header?

如果我在标题中将其设置为默认值,为什么不存在Authorization参数?

1 个解决方案

#1


1  

When you refresh the page all the javascript memory data are gone. You will need to store the bearer token in the browser local storage and get it back from the localstorage and again set it in your common header on page reload.

刷新页面时,所有javascript内存数据都消失了。您需要将承载令牌存储在浏览器本地存储中,然后从本地存储中将其恢复,并在页面重新加载时再次将其设置在公共头中。

You can check the localstorage working here

您可以查看在这里工作的localstorage

#1


1  

When you refresh the page all the javascript memory data are gone. You will need to store the bearer token in the browser local storage and get it back from the localstorage and again set it in your common header on page reload.

刷新页面时,所有javascript内存数据都消失了。您需要将承载令牌存储在浏览器本地存储中,然后从本地存储中将其恢复,并在页面重新加载时再次将其设置在公共头中。

You can check the localstorage working here

您可以查看在这里工作的localstorage