jQuery AJAX Reponse Set Cookie Header

我有一个使用REST API的项目。 在Isend登录请求时,他们发送响应作为JSON包含一些数据。 与响应标题中的一起

Access-Control-Allow-Origin:* Connection:keep-alive Content-Length:239 Content-Type:application/json Date:Fri, 19 Oct 2012 06:28:12 GMT Server:Apache/2.2.22 (Amazon) Set-Cookie:session=username; Path=/ 

这里我们有一个Set-Cookie ,但这个cookie没有设置。 我需要设置此cookie,因为对于任何其他API访问,服务器将检查此cookie。

我该如何解决这个问题? jQuery AJAX响应Header Set-Cookie方法的解决方案是什么?

您可以从XMLHTTPRequest获取标头。 这可能有所帮助。 如果这是有效的,请告诉我。

 $.ajax({ type: 'POST', url:'url.do', data: formData, success: function(data, textStatus, XMLHttpRequest){ var cookietoSet=XMLHttpRequest.getResponseHeader('Set-Cookie'); Set_Cookie(cookietoSet.split('=')[0],cookietoSet.split('=')[1],expires, path, domain, secure)//change as per ur needs } error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.getResponseHeader('some_header')); } }); function Set_Cookie( name, value, expires, path, domain, secure ) { // set time, it's in milliseconds var today = new Date(); today.setTime( today.getTime() ); /* if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24 */ if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; } var expires_date = new Date( today.getTime() + (expires) ); document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" ); } 

我认为这里真正的问题是,如果你的浏览器的隐私/ cookie设置设置为偏执,那么一些浏览器(至少FF和IE)似乎忽略了基于XHR的Set-Cookie标头。

这是一些可能对您的用户有用的FF帮助的链接。 http://support.mozilla.org/en-US/kb/fix-login-issues-on-websites-require-passwords

我希望这个8有更好的答案/解决方法(