jQuery AJAX 响应设置 Cookie 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12968611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AJAX Response Set Cookie Header
提问by Jubin Thomas
I have a project which using a a REST API. Here when Isend a login request, they are sending me the response as JSON containg some data. Along with that in Response 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=/
Here we have a Set-Cookie
, but this cookie is not setting. I need this cookie to be setted, because for any other API access, the server will check for this cookie.
这里我们有一个Set-Cookie
,但是这个 cookie 没有设置。我需要设置这个 cookie,因为对于任何其他 API 访问,服务器将检查这个 cookie。
How can I resolve this Issue? What is the solution for jQuery AJAX response Header Set-Cookie method?
我该如何解决这个问题?jQuery AJAX 响应 Header Set-Cookie 方法的解决方案是什么?
回答by thecodeHyman
You can get headers from XMLHTTPRequest. This may help. Let me know if this is working.
您可以从 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" : "" );
}
回答by roBman
I think the real issue here is that if you have your browser's privacy/cookie settings set to paranoid then some browsers (at least FF & IE) seem to ignore XHR based Set-Cookie headers.
我认为这里真正的问题是,如果您将浏览器的隐私/cookie 设置设置为偏执,那么某些浏览器(至少 FF 和 IE)似乎会忽略基于 XHR 的 Set-Cookie 标头。
Here's a link to some FF help that may be useful for your users. http://support.mozilla.org/en-US/kb/fix-login-issues-on-websites-require-passwords
这是一些可能对您的用户有用的 FF 帮助的链接。 http://support.mozilla.org/en-US/kb/fix-login-issues-on-websites-require-passwords
I wish there were a better answer/workaround for this 8(
我希望有一个更好的答案/解决方法 8(