获取响应头jQuery的阿贾克斯后设置Cookie [英] Get response header jquery ajax post Set-Cookie
问题描述
我使用YouTrack我们的跟踪系统。 Youtrack配备了一个休息web服务,你可以打电话来从系统信息。我有问题得到授权,并得到禁止所有的时间。
I am using YouTrack for our tracking system. Youtrack comes with a rest webservice that you can call to get information from the system. I am having problem getting authorized and is getting forbidden all the time.
我做我的职位,他们的登录,我也得到了登录OKrespone,如果我检查的萤火,我可以看到头设置正确,但饼干还没有生成。为此,我需要得到响应头,设置Cookie值。
I do my post to their login, and I get the "login ok" respone, and if I check in firebug I can see that the headers are set correctly, but the cookie does not get created. For that I need to get the value from the response header, Set-Cookie.
该帖子是这样的。
$.post(youTrackLoginUrl, { login: "restUser", password: "qwerty" }, function(data, text, xhr) {
// do something
});
和响应,并要求看起来是这样的。
And the response and request looks like this.
响应报头:
HTTP / 1.1 200 OK 服务器:Apache-狼/ 1.1 设置Cookie:YTJSESSIONID = 91541168A3E0FF9BBB02A8D427D70127;路径= / jetbrains.charisma.main.security.PRINCIPAL=NjVlODRiZTMzNTMyZmI3ODRjNDgxMjk2NzVmOWVmZjNhNjgyYjI3MTY4YzBlYTc0NGIyY2Y1OGVlMDIzMzdjNTpyZXN0VXNlcg;过期=周三,09 - 10月 - 2013格林尼治标准时间9时47分48秒;路径= / 缓存控制:无缓存,无店面,无转换,必重新验证 访问控制 - 允许 - 产地:a.domain.com 访问控制 - 允许-凭据:真 内容类型:应用程序/ XML的,字符集= UTF-8 传输编码:分块 日期:星期二,2012年10月9日9时47分48秒GMT
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: YTJSESSIONID=91541168A3E0FF9BBB02A8D427D70127; Path=/ jetbrains.charisma.main.security.PRINCIPAL=NjVlODRiZTMzNTMyZmI3ODRjNDgxMjk2NzVmOWVmZjNhNjgyYjI3MTY4YzBlYTc0NGIyY2Y1OGVlMDIzMzdjNTpyZXN0VXNlcg; Expires=Wed, 09-Oct-2013 09:47:48 GMT; Path=/ Cache-Control: no-cache, no-store, no-transform, must-revalidate Access-Control-Allow-Origin: a.domain.com Access-Control-Allow-Credentials: true Content-Type: application/xml;charset=UTF-8 Transfer-Encoding: chunked Date: Tue, 09 Oct 2012 09:47:48 GMT
要求:
POST / REST /用户/登录HTTP / 1.1 主持人:b.eelab.se 用户代理:Mozilla的/ 5.0(Windows NT的6.1; WOW64; RV:14.0)的Gecko / 20100101 Firefox的/ 14.0.1 接受: / 的 接受语言:SV-SE,SV; Q = 0.8,EN-US; Q = 0.5,连接; Q = 0.3 接受编码:gzip,紧缩 连接:保持活动 内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8 引用站点:<一href="http://intra.eelab.se/kontakt/it-vad-jobbar-vi-pa-nu/">http://intra.eelab.se/kontakt/it-vad-jobbar-vi-pa-nu/ 内容长度:30 原产地: http://a.domain.com 杂注:无缓存 缓存控制:无缓存
POST /rest/user/login HTTP/1.1 Host: b.eelab.se User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1 Accept: / Accept-Language: sv-se,sv;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://intra.eelab.se/kontakt/it-vad-jobbar-vi-pa-nu/ Content-Length: 30 Origin: http://a.domain.com Pragma: no-cache Cache-Control: no-cache
我需要的Set-Cookie值,以在网站上创建的cookie中。我能做些什么才达到这一点?
I need the Set-Cookie value to create the cookie on the site. What can I do to achive this?
/干杯。
推荐答案
试试这个:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
$.post(youTrackLoginUrl, { login: "restUser", password: "qwerty" }, function(data, text, xhr) {
data_set_cookie = data.match(/Set-Cookie:\s([^;]+);/)[1];
createCookie(data_set_cookie.split("=")[0],data_set_cookie.split("=")[1],365); //sets cookie for 1 year
});
这篇关于获取响应头jQuery的阿贾克斯后设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!