我有一个 Android webview,我相信它具备访问和使用本地存储所需的一切条件,但是当我尝试使用本地存储时,在控制台中看到了一个“Access denied”的错误。
Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
有人能发现问题吗?
JavaScript 代码:
function localStorageTest() {
// Check browser support
if (typeof(Storage) != "undefined") {
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("console").innerHTML = localStorage.getItem("lastname");
} else {
document.getElementById("console").innerHTML = "Sorry, your browser does not support Web Storage...";
}
}
以下是 Android 代码:
// Enable javascript
WebSettings settings = getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
// Create database folder
String databasePath = getContext().getDir("databases", Context.MODE_PRIVATE).getPath();
getSettings().setDatabasePath(databasePath);
getContext().getPackageName() + "/databases/");
settings.setAppCacheEnabled(true);
settings.setSaveFormData(true);;
settings.setLoadWithOverviewMode(true);
settings.setSaveFormData(true);
getContext().getPackageName() + "/databases/");
。 - Cory Robinson