Jub*_*mas 5 cookies jquery httpresponse jquery-cookie
我有一个使用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=/
Run Code Online (Sandbox Code Playgroud)
这里我们有一个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" : "" );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22931 次 |
| 最近记录: |