【问题标题】:When called opendatabase in angular factory, it thows security_err dom exception 18 error在角度工厂中调用opendatabase时,它会抛出security_err dom异常18错误
【发布时间】:2016-01-22 09:29:51
【问题描述】:

我正在 vs2015 中开发 Angularjs 应用程序。 我想通过工厂函数在 websql 中打开数据库。在涟漪模拟器中测试应用程序时,它可以完美运行(在 android OS 4.4 及更高版本中也可以完美运行),但在 android OS 4.2 及更低版本中测试应用程序时,它会抛出security_err dom exception 18。 更奇怪的是,当我在同一个数据库中打开一个简单的 java 脚本文件时,它在波纹和所有设备中都可以完美运行。

这是我的工厂代码:

angular.module('InnkeyAlert.services', ['InnkeyAlert.config'])

// DB wrapper

.factory('DB', function ($q, DB_CONFIG) {

try {
    var self = this;
    self.db = null;

    self.init = function () {
        alert("DB init start!");
        // Use self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name}); in production
        self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1);
        alert("DB open!");
        angular.forEach(DB_CONFIG.tables, function (table) {
            var columns = [];

            angular.forEach(table.columns, function (column) {
                columns.push(column.name + ' ' + column.type);
            });

            var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
            self.query(query);
            //console.log('Table ' + table.name + ' initialized');
        });
        alert("table created !");
    };

    return self;

} catch (e) {
    //alert("Db factory: "+e.message);
}

})

这是我的 index.js 文件代码:

    var app = {

        // Application Constructor
        initialize: function () {
            this.bindEvents();
        },
        // Bind Event Listeners
        //
        // Bind any events that are required on startup. Common events are:
        // 'load', 'deviceready', 'offline', and 'online'.
        bindEvents: function () {
            alert("hi");
            document.addEventListener('deviceready', this.onDeviceReady, false);        
        },

        // The scope of 'this' is the event. In order to call the 'receivedEvent'
        // function, we must explicitly call 'app.receivedEvent(...);'
        onDeviceReady: function () {
            app.receivedEvent('deviceready');
            alert("Device ready end...");
            try {
                angular.injector(['InnkeyAlert']).get('DB').init();
                alert("inti db called...");
            } catch (e) {
                alert("index 101: " + e.message);
            }
        },
        // Update DOM on a Received Event
        receivedEvent: function (id) {

        }
    };

【问题讨论】:

  • security_err dom exception 18 在哪一行被抛出?请添加 stacktrace 或 logcat 输出。
  • @JohannesJander - 在我的工厂 init() -> self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1);

标签: javascript angularjs cordova web-sql


【解决方案1】:

这是低于 4.4 的 Android 版本的问题。已回复here

您可以从 WebView 中的 file:// URL 使用 Web SQL。科尔多瓦人设法做到了。您必须做的只有三件事:

1) 调用 setDatabaseEnabled() (duh):

2) 设置数据库文件路径。是的,这在 Android 4.4 中已被弃用,但如果您想避免之前 Kitkat 中的 DOM 异常 18,则需要它:

3) 设置 onExceededDatabaseQuota 处理程序。是的,它在 Android 4.4 中已被弃用,等等等等。

【讨论】:

  • 这个放在哪里?我的代码在 javascript 上。上面看起来像 c# 或 vb.net 代码。
  • 那是 Java。你不能在javascript中做到这一点。如果你绝对需要支持 Android 4.2,你需要使用 Java 来准备 webview。
  • 但是如何将该代码与我的 java 脚本代码集成。?有什么解决办法吗?
  • Cordova 等技术。您无法在普通的 Web 应用程序中做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-11
  • 2011-07-27
  • 2012-04-08
  • 2011-02-11
  • 2023-04-09
  • 2014-12-13
  • 1970-01-01
相关资源
最近更新 更多