【发布时间】:2013-07-30 00:33:08
【问题描述】:
我正在尝试使用 indexedDB 存储离线数据,然后在连接时上传数据。在以下代码中,使用循环从 indexedDB 中读取数据,并为表(存储)中的每条记录(对象)创建一个 JSON 对象并将其发布到 PHP 文件中。然而,这个 indexedDB 循环只执行一次。这是因为 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"); };
【问题讨论】:
标签: php javascript json indexeddb