Angularjs $ HTTP似乎不明白"设置Cookie"在响应 [英] Angularjs $http does not seem to understand "Set-Cookie" in the response
问题描述
我有认证护照模块前的NodeJS preSS REST API。
一个登录方法(GET)返回头一个cookie。当我把它从Chrome中正常工作,我的Cookie设置在浏览器中。
但是,如果我把它通过从$ HTTP Angularjs,cookie不会设置。
<$p$p><$c$c>Set-Cookie:connect.sid=s%3Ad7cZf3DSnz-IbLA_eNjQr-YR.R%2FytSJyd9cEhX%2BTBkmAQ6WFcEHAuPJjdXk3oq3YyFfI;路径= /;仅Http正如你可以在上面看到,在Set-Cookie是present在HTTP服务响应的头。
也许仅Http可能是这个问题的根源?如果是的话,我能怎样改变?这里是我的前preSS配置:
app.configure(函数(){
app.use(allowCrossDomain);
app.use(如press.bodyParser());
app.use(如press.cookieParser())
app.use(如press.session({秘密:这是一个秘密'}));
app.use(闪光灯());
//初始化护照
app.use(passport.initialize());
app.use(passport.session());
app.set('口',process.env.PORT || 8080);
});
感谢您的帮助。
请务必配置为使用凭据您的$ HTTP请求。
从XHR文档:
<一个href=\"https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS\">https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
最有趣的功能由双方XMLHtt prequest暴露和
访问控制是使有持证请求的能力
认识到HTTP Cookie和HTTP验证信息。通过
默认情况下,跨站点XMLHtt prequest调用,浏览器不会
发送凭据。一个具体的标志,必须在XMLHtt prequest设置
被调用时的对象。
块引用>您可以使用选项对象来设置使用的凭据,请参阅
http://docs.angularjs.org/api/ng.$http
例如:
$ HTTP({withCredentials:真实,...})获得(...)
I have a nodejs express REST api with Passport module for authentication. A login method (GET) returns a cookie in the header. When I call it from Chrome it works fine, my cookie is set in my browser.
But if I call it through $http from Angularjs, the cookie is not set.
Set-Cookie:connect.sid=s%3Ad7cZf3DSnz-IbLA_eNjQr-YR.R%2FytSJyd9cEhX%2BTBkmAQ6WFcEHAuPJjdXk3oq3YyFfI; Path=/; HttpOnly
As you can see above, the Set-Cookie is present in the header of the http service response.
Perhaps HttpOnly may be the source of this problem? If yes, how can I change it? Here is my express configuration :
app.configure(function () { app.use(allowCrossDomain); app.use(express.bodyParser()); app.use(express.cookieParser()) app.use(express.session({ secret: 'this is a secret' })); app.use(flash()); //passport init app.use(passport.initialize()); app.use(passport.session()); app.set('port', process.env.PORT || 8080); });
Thank you for your help
解决方案Make sure to configure your $http request to use credentials. From the XHR documentation:
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
The most interesting capability exposed by both XMLHttpRequest and Access Control is the ability to make "credentialed" requests that are cognizant of HTTP Cookies and HTTP Authentication information. By default, in cross-site XMLHttpRequest invocations, browsers will not send credentials. A specific flag has to be set on the XMLHttpRequest object when it is invoked.
You can use the options object to set the use of credentials, see
http://docs.angularjs.org/api/ng.$http
Example:
$http({withCredentials: true, ...}).get(...)
这篇关于Angularjs $ HTTP似乎不明白&QUOT;设置Cookie&QUOT;在响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!