我正在使用PhoneGap和jQuery Mobile。我試圖從遠程位置獲取一些JSON數據,然後用它填充本地WebSQL數據庫。這裏是我的JS功能:WebSQL事務不會在JS回調函數中運行
function getLocations() {
var tx = window.openDatabase('csdistroloc', '1.0', 'Distro DB', 1000000);
tx.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS locations'); //this line works!
tx.executeSql('CREATE TABLE IF NOT EXISTS locations (id, name, address, postalcode, phone, category)'); //this line works!
$.ajax({
url: "http://mydomain.com/api.php",
dataType: 'json',
data: { action: "getlocations" },
success: function(data) {
tx.executeSql("INSERT INTO locations (id, name, address, postalcode, phone, category) VALUES (2,'cheese','232','seven',5,6)"); //this line produces an error
}});
}, dberror, dbsuccess);
}
運行上面的功能給我一個錯誤「INVALID_STATE_ERR:DOM異常11」就行了上面提到的。當我實際嘗試使用返回的JSON數據插入數據時,它會執行相同的操作。我也嘗試了與.getJSON技術完全相同的結果。
任何意見將不勝感激!
僅供參考,我寫了基本[相同的答案](http://stackoverflow.com/questions/12265003/phonegap-invalid-state-err-dom-exception-11/27168338#27168338)到另一個問題。 – Celada 2014-11-27 10:29:38
該規範的相關部分在這裏 - http://www.w3.org/TR/webdatabase/#transaction-steps – 2015-08-26 14:51:32
你應該檢查下一個答案,你可能會添加第三點爲一個可能的永久解決方案:) – TechMaze 2015-09-16 19:35:14