ngResource在每个请求上动态设置标头 [英] ngResource Dynamically Set Header on Each Request

查看:149
本文介绍了ngResource在每个请求上动态设置标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注册服务后,我无法使用ngRessource编辑标头.

I see it isn't possible to edit headers with ngRessource after the service has been registered.

对于通过autorization标头中的jwt令牌使用身份验证的人来说这不是一个大问题吗?

Isn't this a big problem for people using authentication via jwt tokens in the autorization header ?

我还必须能够在每个请求上动态设置标头:

I have to be able too set headers dynamically on each request :

  {     Authorization: 'Bearer '+ myAuthService.getToken()     }

$ http没什么问题.

It poses no problem what so ever with $http.

注册服务后,真的不可能通过ngRessource在标头中设置令牌吗?

Is it really impossible to set tokens in the headers via ngRessource after the service has been registered ?

推荐答案

我还必须能够在每个请求上动态设置标头:

I have to be able too set headers dynamically on each request :

要在每个请求上设置标头,请将标头设置为函数:

To set the header on each request, make the header a function:

{ Authorization: function(config) {
                     return 'Bearer '+ myAuthService.getToken(); 
                 }
}

从文档中:

  • headers – {Object} –字符串或返回代表要发送到服务器的HTTP头的字符串的函数的映射.如果函数的返回值为null,则不会发送标头.函数接受配置对象作为参数.
  • headers – {Object} – Map of strings or functions which return strings representing HTTP headers to send to the server. If the return value of a function is null, the header will not be sent. Functions accept a config object as an argument.

通过提供一个函数,将在每个请求上计算标头值.

By furnishing a function, the header value will be computed on each request.

这篇关于ngResource在每个请求上动态设置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆