XSRF头不是AngularJS被设置 [英] XSRF headers not being set in AngularJS
问题描述
我开发一个Django + AngularJS应用程序,其中角部分没有被Django的服务。
I'm developing a DJANGO + AngularJS application, where the angular part is not being served by django.
我定了角 $ httpProvider
如下:
myApp = angular.module('myApp', [])
myApp.config(['$httpProvider',
function(provider){
provider.defaults.xsrfCookieName = 'csrftoken';
provider.defaults.xsrfHeaderName = 'X-CSRFToken';
}
那么,做任何POST之前,我做一个GET其中设置cookie。我可以通过浏览器确认Cookie设置:
Then, before doing any POST, I do a GET which sets the cookie. I can confirm through Chrome that the cookie is set:
set-cookie:csrftoken=hg88ZZFEdLPnwDdN1eiNquA8YzTySdQO; expires=Tue, 19-Aug-2014 12:26:35 GMT; Max-Age=31449600; Path=/
(它是可见在Chrome开发者工具的资源/饼干/本地主机)
(it's visible in resources/cookies/localhost in the Chrome developer tools)
然而,当我做了自检,没有 X-CSRFToken
头被设置
However when I do a POST, no X-CSRFToken
header is being set
这是POST所记录的浏览器:
this is the POST as recorded by Chrome:
POST /data/activities/search HTTP/1.1
Host: localhost:14080
Connection: keep-alive
Content-Length: 2
Accept: application/json, text/plain, */*
Origin: http://localhost:14080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:14080/public/html/main.html?codekitCB=398694184.799418
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: csrftoken=hg88ZZFEdLPnwDdN1eiNquA8YzTySdQO
为什么没有头被设置?还有什么应该怎样做才能激活此功能?
Why is no header being set? What else should I do to activate this functionality?
(边注:如果我手动传递在$ HTTP()调用的头,POST请求正常工作。所以这个问题真的不是被AngularJS设置页眉)
(side note: if I manually pass the header in the $http() call, the POST request works fine.. therefore the problem is really the header not being set by AngularJS)
推荐答案
很简单的答案:它只能从1.2.0版本,这是目前一个候选发布版
very simple answer: it's only available from version 1.2.0, which is at the moment a release candidate.
这篇关于XSRF头不是AngularJS被设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!