あなたがのIndexedDBのような小さなDB(A.Wolffにより示唆されるように)の使用を避けたい場合は、テキストファイルを作成し、AJAXを介してアクセスすることができます
var xhr = new XMLHttpRequest();
xhr.open('GET', 'path/to/your/text/file', false);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == '200') {
// the responseText property is the content of the file
// then you can do whatever you want with the file
console.log('file', xhr.responseText);
}
};
xhr.send(null);
することができますまた、コールバックで関数の中で、このコードを配置:
function loadAjax(file, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', file, false);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == '200') {
callback(xhr.responseText);
}
};
xhr.send(null);
}
そしてそれを呼び出す:
loadAjax('path/to/your/text/file', function(response) {
console.log('file', response); // content of file
});
より現代的な解決法(fetch、古いブラウザではポリフィルを使用)または外部ライブラリ(jQuery、スーパーユーザなど)を使用してください。
さらに、jsonファイルにデータを格納し、それを引き続きajaxでフェッチしながら簡単に解析できます。例えば:
loadAjax('path/to/your/json/file', function(response) {
console.log('file', JSON.parse(response)); // content of file
});
私は[のIndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) –
を使用したい外部せずにこれを行う方法はありませんAPI? – Squiller
IndexedDBは外部APIではなく、WebAPIの一部であり、ネイティブではほとんどのブラウザ(少なくとも最新のもの)に実装されています。 – mdziekon