获取响应头jquery ajax post Set-Cookie

时间:2022-10-08 22:13:19

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.

我在用你的追踪系统。Youtrack附带一个rest web服务,您可以通过它从系统中获取信息。我在获得授权上有困难,而且一直被禁止。

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.

我把我的帖子发到他们的登录名上,然后我得到了“登录确认”的回复,如果我检查了firebug,我可以看到标题设置正确,但是cookie没有被创建。为此,我需要从响应头、Set-Cookie中获取值。

The post looks like this.

这篇文章是这样的。

      $.post(youTrackLoginUrl, { login: "restUser", password: "qwerty" }, function(data, text, xhr) {

      // do something 

      });

And the response and request looks like this.

响应和请求是这样的。

Response headers:

响应标头:

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

HTTP/1.1 200 OK服务器:Apache-Coyote/1.1 Set-Cookie: YTJSESSIONID=91541168A3E0FF9BBB02A8D427D70127;路径= / jetbrains.charisma.main.security.PRINCIPAL = NjVlODRiZTMzNTMyZmI3ODRjNDgxMjk2NzVmOWVmZjNhNjgyYjI3MTY4YzBlYTc0NGIyY2Y1OGVlMDIzMzdjNTpyZXN0VXNlcg;到期=结婚,09 - 10月- 2013 09:47:48格林尼治时间;路径=/ Cache-Control:无缓存,无存储,无转换,必须重新验证访问-控制-允许来源:a.domain.com访问-控制-允许凭证:真正的内容类型:应用/xml

Request:

要求:

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

POST /rest/user/login HTTP/1.1主机:b.eelab。se用户代理:Mozilla/5.0 (Windows NT 6.1);WOW64;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- urlenform - code;utf - 8字符集=推荐人:http://intra.eelab。se/kontakt/it-vad-jobbar-vi-pa-nu/内容长度:30来源:http://a.domain.com Pragma: no-cache - control: no-cache

I need the Set-Cookie value to create the cookie on the site. What can I do to achive this?

我需要Set-Cookie值来创建站点上的cookie。我该怎么做才能实现这个目标呢?

/Cheers.

/欢呼。

1 个解决方案

#1


3  

Try 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

});

#1


3  

Try 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

});