Ber*_*rnd 84 cookies xmlhttprequest cordova
我正在开发一个使用服务器会话的PhoneGap应用程序.它需要cookie来处理会话.此外,还应处理来自负载均衡器的cookie.所以没有办法解决.你如何处理PhoneGap应用程序中的Cookie?
我已经完成了一些研究:
Tia*_*vêa 67
朋友,我已经尝试过使用带有phonegap的cookie而没有成功.解决方案是使用localStorage.
关键快速示例:
var keyName = window.localStorage.key(0);
Run Code Online (Sandbox Code Playgroud)
设置项目快速示例:
window.localStorage.setItem("key", "value");
Run Code Online (Sandbox Code Playgroud)
获取项目快速示例
var value = window.localStorage.getItem("key");
// value is now equal to "value"
Run Code Online (Sandbox Code Playgroud)
删除项目快速示例:
window.localStorage.removeItem("key");
Run Code Online (Sandbox Code Playgroud)
清除快速示例:
window.localStorage.clear();
Run Code Online (Sandbox Code Playgroud)
如果您在移动设备和网络上使用javascript,则可以使用此代码来检测该环境:
var wl = window.location.href;
var mob = (wl.indexOf("android")>0);
Run Code Online (Sandbox Code Playgroud)
参考文献:http : //docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html#localStorage http://cordova.apache.org/docs/en/6.x/cordova/storage/storage.html #页面TOC源
请注意:在iOS上使用匿名导航可能会使localstorage无法像spected那样工作.一个对我来说很好的简单测试:
$(document).ready(function () {
try {
localStorage.setItem('test', '1');
} catch (Err) {
if (Err.message.indexOf('QuotaExceededError') > -1) {
// Tell the user they are in anonymous mode
// Sugest it to go to https://support.apple.com/pt-br/HT203036 to get help to disable it
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
与您类似,我想在我的应用程序中使用服务器设置的 cookie,以便我的应用程序与网络保持一致,并且不需要单独的设备 ID 或其他身份验证方法。
我发现的是以下内容:
$.get()或$.post())设置的 Cookie不会持续存在使 cookie 持久化的方法是使用inAppBrowser插件。
首先,设置一个简单的服务器,它接受您想要保留的 GET 参数键值参数。我是一个 python/tornado 人,所以我的服务器可能看起来像:
class PersistCookieHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
token = self.get_argument('token')
self.set_secure_cookie('token',token)
Run Code Online (Sandbox Code Playgroud)
然后,在您的应用中:
function persistToken(token,success_cb, error_cb) {
// replace with your URL
url = 'SECURE_SERVER_URL_THAT_HANDLES_SET_COOKIE';
// _blank tells inAppBrowser to load in background (e.g., invisible)
var ref = window.open(url, '_blank', 'location=no,toolbar=no');
// attach a listener to the window which closes it when complete
ref.addEventListener('loadstop', function(event) {
ref.close();
success_cb(); // call our success callback
});
// attach a listener for server errors
ref.addEventListener('loaderror', function(event) {
// call our error callback
error_cb(event);
});
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以按如下方式调用它:
persistToken(
someToken,
function() {
console.log("token persisted");
},
function() {
console.log("something went wrong);
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71216 次 |
| 最近记录: |