convert indexedb objecstore to json for send data with post

272 Views Asked by At

i must send data of an objectstore with post to a php file but i can't transform objectstore to send it.

Thanks for help

    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(????);
    
    }; 
1

There are 1 best solutions below

0
Simone Bianchi On

I have found a solution for sending a JSON file, with an XMLHttpRequest transforming automatically an 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));

    };
};

};