Where cookie is managed in phonegap app with jquery?

守給你的承諾、 提交于 2019-12-20 08:31:04

问题


My native iphone app, developed with phonegap with jquery (so its browser based), can log in to web server and once logged in users can access their resources. The server sets session id in cookie once user is authenticated.

I do not have any trouble with this scheme but I am wondering where the cookie is stored because when I do alert(document.cookie), it returns empty string.

Is it possible that ajax function in jquery manages the cookie internally and send it along for every request to the same domain?


回答1:


If you want your app to automatically manage cookies for you add this to your appDelegate.m file:

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage 
                                      sharedHTTPCookieStorage]; 
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; 

If you want to manage the session information by yourself, you can do all ajax requests like this:

var request =   {
                url: my_server_url,
                success: function(response, status, request) {
                    var header = request.getAllResponseHeaders();
                    var match = header.match(/(Set-Cookie|set-cookie): (.+?);/);
                    if(match)
                        my_saved_cookie = match[2];
                },
                }

if(my_saved_cookie)
    request.headers = { Cookie: my_saved_cookie };

$.ajax(request);

In my app I was managing the session cookies by myself using the second method until I discovered the first method.



来源:https://stackoverflow.com/questions/5124300/where-cookie-is-managed-in-phonegap-app-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!