Android上的Cordova; getResponseHeader(“Set-Cookie”)仅返回最后一个cookie而不返回其余cookie。

时间:2013-09-24 08:18:10

标签: android jquery ajax cordova

在我们的Android平台上,在Cordova项目getResponseHeader("Set-Cookie")中,只返回响应中的最后一个cookie,而不是像在iOS平台中那样返回所有cookie。

$.ajax({
type: "POST",
cache:false,
url: url,
contentType: "application/json; charset=utf-8",

data: dotJSON,
success: function( data, textStatus, req ) {
    $.mobile.loading( 'hide', {
        textVisible: false,
        theme: 'a',
        html: ""
    });

    var header = req.getAllResponseHeaders();

    //console.log(header);
    console.log(req.getResponseHeader("Set-Cookie")); // this only prints the following
   // 'Set-Cookie: DIdN=[...]; path=/'

   [...]
});

在iOS平台中,console.log(req.getResponseHeader("Set-Cookie"));还会打印一个ASPXAUTH-cookie,我们需要将该会话与服务器保持一致。

我在SO,PhoneGap Google小组和Cordova问题跟踪器上都搜索过,发现人们和我有同样的问题,但他们都没有得到答复。

任何人都对Android平台上可能出现的问题有所了解,但对iOS平台没有?

1 个答案:

答案 0 :(得分:2)

我通过将响应正文中的cookie作为字符串发送然后将其存储在本地存储中来解决此问题:

window.localStorage.setItem("Cookies", data);

然后我使用

在所有的ajax请求中设置它们

beforeSend:setHeader

function setHeader(xhr){
    xhr.setRequestHeader('Set-Cookie', window.localStorage.getItem("Cookies"));
}

希望这会帮助那些有同样问题的人。