Html 科尔多瓦的 HTML5 本地存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14804874/
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
HTML5 localstorage in Cordova
提问by copilot0910
Does HTML5 Local Storage work in Cordova / PhoneGap? I am trying to use it, both the HTML5 way and the way specified in the docs. Neither work.
HTML5 本地存储在 Cordova / PhoneGap 中工作吗?我正在尝试使用它,包括 HTML5 方式和文档中指定的方式。都不工作。
Specifically, I am trying to use an ajax query result for local storage. I have tested the query, and it works.
具体来说,我正在尝试将 ajax 查询结果用于本地存储。我已经测试了查询,并且它有效。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi">
<title>Hello World</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript">
</script>
<script type="text/javascript">
$(document).ready(function () {
$("form").submit(function () {
var uname = document.getElementById("username").value;
var pword = document.getElementById("password").value;
var postData = {
username: uname,
password: pword
};
$.ajax({
url: "http://www.yellowcabsavannah.com/test.php",
type: "POST",
data: postData,
async: false,
dataType: 'json',
cache: false,
success: function (data) {
localStorage.uname = data.username;
localStorage.pword = data.password;
alert(localStorage.uname);
}
}
});
return false;
});
});
</script>
</head>
<body>
<form action="">
<input type='text' id="username" name="username" placeholder="Username">
<br>
<input type='password' id="password" name="password" placeholder="password">
<br>
<input type="submit" id="submit" value="Login">
</form>
</body>
回答by SHANK
I've used local storage like this:
我使用过这样的本地存储:
// To store a value
window.localStorage.setItem('key', value);
// To retrieve a value
value = window.localStorage.getItem('key');
// To delete a storage
window.localStorage.removeItem('key');
Hope that helps.
希望有帮助。
回答by reddington
i am using cordova to build an Android app and i am able to save local storage variables with window.localStorage['username'], like:
我正在使用cordova 来构建一个Android 应用程序,我可以使用window.localStorage['username'] 保存本地存储变量,例如:
window.localStorage['username'] = data.username
window.localStorage['username'] = data.username
it is like a PHP associative array.
它就像一个 PHP 关联数组。
Hope that helps
希望有帮助