IndexedDB 将indexedb对象存储转换为json以便使用post发送数据

u4dcyp6a  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(217)

我必须用post发送objectstore的数据到一个php文件,但我不能转换objectstore来发送它。
多谢帮忙

var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "../php/sincDATI.php", true);
    var request = indexedDB.open("rsapp",1, "persistent");
    request.onsuccess = function (evt) {
      var db = request.result;
      var tx = db.transaction(pTable,"readwrite");
      var store = tx.objectStore(pTable); 

      *** TRAFORM store in json  ????
    
      xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

      xhttp.onreadystatechange = function() {
        
        if (this.readyState == 4 && this.status == 200) {

            
            
        };
    
    };
    
    xhttp.send(????);
    
    };
zysjyyx4

zysjyyx41#

我找到了一个发送JSON文件的解决方案,其中XMLHttpRequest自动转换为indexedDB objectStore

function sendData(pTable,pMess) {

var xhttp = new XMLHttpRequest();
var sendArray=[];
var request = indexedDB.open("dbapp",1, "persistent");
   
request.onsuccess = function (evt) {

    var db = request.result;
    var tx = db.transaction(pTable,"readwrite");
    var store = tx.objectStore(pTable);            
    var cursorRequest = store.getAll();
        
    cursorRequest.onsuccess = function(event) {
        var cursor=event.target.result;
 
        for(var item of cursor) {
            
          sendArray.push(item);

        };

        xhttp.open("POST", "../php/sincDATI.php", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                $("#lblMessaggi").html("OK!"+ xhttp.responseText);
                    
            };
            
        };

        xhttp.send("pArr="+JSON.stringify(sendArray));

    };
};

};

相关问题