Javascript 将静态json文件放入indexedDB
我试图存储服务器端存储的json文件中的数据,我使用angular js和html,我可以通过在indexeddb函数中输入json数据来添加json数据,但是我需要使用http.get提取提要,然后将其放入数据库,这是我的代码Javascript 将静态json文件放入indexedDB,javascript,json,angular,storage,indexeddb,Javascript,Json,Angular,Storage,Indexeddb,我试图存储服务器端存储的json文件中的数据,我使用angular js和html,我可以通过在indexeddb函数中输入json数据来添加json数据,但是我需要使用http.get提取提要,然后将其放入数据库,这是我的代码 $scope.save_data = function save_data(event) { let indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || wi
$scope.save_data = function save_data(event) {
let indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
let open = indexedDB.open("myDB", 1);
open.onupgradeneeded = function () {
let db = open.result;
let store = db.createObjectStore("the_Times", { keyPath: "id", value: "Article1" });
let store1 = db.createObjectStore("the_Times1", { keyPath: "id1", value: "Article2" });
let store2 = db.createObjectStore("the_Times2", { keyPath: "id2", value: "Article3" });
let index = store.createIndex("Article1", ["news.Article1"]);
let index1 = store1.createIndex("Article2", ["news.Article2"]);
let index2 = store2.createIndex("Article3", ["news.Article3"]);
open.onsuccess = function (e) {
let db = open.result;
let tx = db.transaction("the_Times", "readwrite");
let tx1 = db.transaction("the_Times1", "readwrite");
let tx2 = db.transaction("the_Times2", "readwrite");
let store = tx.objectStore("the_Times");
let store1 = tx1.objectStore("the_Times1");
let store2 = tx2.objectStore("the_Times2");
let index = store.index("Article1");
let index1 = store1.index("Article2");
let index2 = store2.index("Article3");
console.log('added successfully');
//this is where i want to pull data from a url containing the json data and put it in the DB
store.put({"JSON string is here... Should be url json data"});
store1.put({"JSON string is here"});
store2.put({"JSON string is here"});
let transaction = db.transaction(["the_Times"], "readonly");
let objectStore = transaction.objectStore("the_Times", "the_Times1", "the_Times2");
let cursor = objectStore.openCursor();
cursor.onsuccess = function (e) {
let res = e.target.result;
if (res) {
console.log(res.key);
console.dir(res.value);
res.continue();
}
tx.oncomplete = function () {
db.open();
};
}
}
}
};
你可以用答案编辑它(杰克;)但是谢谢我解决了这个问题,就在我的indexedDB代码之前,我添加了一个http.get函数<代码>$http.get(“您的JSON url”)。然后(函数(响应){editionData=response.data;$scope.editionSections=editionData.Sections;$scope.editionSectionsNew=editionData.Custom;$scope.articleData=editionData.Articles;$scope.articleDataUn=editionData.UnsortedData.News;});然后在indexedDB的检索代码中添加了editionData…
cursor.value.editionData.UnsortedData.Data.News.Articles[1]。Image