Restangular: How to read cookie and add to Headers at each query

只谈情不闲聊 提交于 2019-12-11 09:28:57

问题


I'm trying to read a token from a cookie and ad it to the headers at each query.

I tried this:

Restangular.setDefaultHeaders({ Authorization: "Token " + $cookieStore.get('token') })

It works, but the cookie only gets read once. At page load. Then if the value of the cookie changes, it keeps the first value instead of sending the updated value.

Any idea how I can read the cookie each time?


回答1:


You are passing a string, which is evaluated at the time the code is executed. It will never change as you have found out.

Instead you will need to pass a function, like this -

Restangular.setDefaultHeaders({ Authorization: function() { return "Token " + $cookieStore.get('token'); } })

Which will then be evaluated each time the value is actually used.



来源:https://stackoverflow.com/questions/29696406/restangular-how-to-read-cookie-and-add-to-headers-at-each-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!