indexedDB を使用してオフライン データを保存し、接続時にデータをアップロードしようとしています。次のコードでは、ループを使用して indexedDB からデータを読み取り、テーブル (ストア) 内の各レコード (オブジェクト) に対して JSON オブジェクトが作成され、PHP ファイルにポストされます。ただし、この indexedDB ループは 1 回しか実行されません。これは、JSON オブジェクトが非同期でサーバーに送信されたためですか?
var trans = LocalDB.indexedDB.db.transaction(storename,
IDBTransaction.READ_WRITE);
var store = trans.objectStore(storename);
var keyRange = IDBKeyRange.lowerBound(0);
var cursorRequest = store.openCursor(keyRange);
cursorRequest.onsuccess = function (e) {
var result = e.target.result;
var obj = new Object;
obj.name = result.value.Name;
obj.Date = result.value.Date;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
alert(xmlhttp.responseText);//problem: only shown once
result.continue();
}
};
xmlhttp.open("POST", "upload.php");
xmlhttp.setRequestHeader("Content-type", "application/json", true);
xmlhttp.send(JSON.stringify(obj));
};
cursorRequest.onerror = function (e) { alert("Error uploading"); };