如何从AngularJS我的应用程序的配置设置$ httpProvider默认的标题? [英] How to set $httpProvider default headers from my app config in AngularJS?

查看:171
本文介绍了如何从AngularJS我的应用程序的配置设置$ httpProvider默认的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的

$httpProvider.defaults.headers.common['X-CSRF-Token'] = $cookie.auth_token; 

在我的应用我的.config部分,但它似乎并不像我可以访问文件/饼干,只是还没有?有没有更好的地方设置此?

in my .config section of my app, but it doesn't seem like I can access the document/cookies just yet? Is there a better place to set this?

我这样做,因为我存储我的用户在cookie的auth_token所以他们不需要登录每次使用自己的移动应用程序的时间。

I'm doing this because I'm storing my users auth_token in a cookie so they don't need to login every time they use my mobile app.

谢谢!

推荐答案

描述$ HTTP文档

...角度提供了一种机制来对抗XSRF。当执行XHR
  请求,$ http服务从一个名为cookie的读取令牌
  XSRF-TOKEN并将其设置为HTTP标头的X XSRF-TOKEN。

... Angular provides a mechanism to counter XSRF. When performing XHR requests, the $http service reads a token from a cookie called XSRF-TOKEN and sets it as the HTTP header X-XSRF-TOKEN.

...要充分利用这一点,你的服务器需要设置一个令牌
  第一个HTTP的JavaScript可读会话cookie叫XSRF-TOKEN
  GET请求

... To take advantage of this, your server needs to set a token in a JavaScript readable session cookie called XSRF-TOKEN on the first HTTP GET request

所以,如果你在cookie名XSRF-TOKEN设置CSRF令牌则需要对角侧没有调整。和你的code应该工作原样。

So if you set your CSRF token in cookie name XSRF-TOKEN then no adjustments are needed on Angular side. and your code should work as is.

这篇关于如何从AngularJS我的应用程序的配置设置$ httpProvider默认的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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