从索引数据库获取所有数据并使用post方法插入服务器端

时间:2016-12-02 18:47:59

标签: javascript jquery ajax database indexeddb

我有一个索引数据库,其中包含一些数据。我正在获取这些数据,并希望通过服务器端推送这些数据。

第1步:获取这些数据

   var transaction = db.transaction(["customer"], "readonly");  
   var obj = transaction.objectStore("customer");
   var cursorRequest = obj.openCursor();


   cursorRequest.onsuccess = function(evt) {                    
    var cursor = evt.target.result;
    if (cursor) {
        cust_ids.push(cursor.value);
        cursor.continue();
    }
  };

以上将为我提供IDB中的任何数据。现在,我将迭代这些记录,以便将其插入服务器端数据库。

第2步: 循环播放

for (var i=0; i < cust_ids.length;i++){
   customers += JSON.stringify(cust_ids[i]);       
  }
  console.log("from cust" + customers);

第3步: Ajax Call: 现在,我们将进行一个ajax调用,它将使用post方法在服务器端推送这些数据。

  $.ajax({
  url: 'myurl',
   type: 'POST',
  data:customers,//here it only push one record to server though i have       whole data in above console.
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
 // async: false,
  success: function(msg) {
    console.log(msg);
  }
  }); 

我所做的只是插入第一条记录的问题。我能够获取所有数据,但无法将所有数据推送到服务器端数据库。

Json String:

{"cmp_id":131,"cust_id":131,"name":"pranav","zip":"6979","mobile":"9749797","email":"efe@gmail.com","birth_date":"2016-11-10T05:00:00Z"}    {"cust_id":10001,"name":"Edward Logan","add1":"1 Harborside Drive","city":"Flushing","state":"New York","country":"USA","zip":"380061","mobile":"9974682025","email":"edward.logan@email.com","gender":"MALE","bod":"1985-12-31T05:00:00Z","anv_date":"2012-11-20T05:00:00Z"}{"cust_id":10002,"name":"Barry Hoppe","add1":"45020 Aviation Drive","city":"Sterling","state":"Virginia","country":"USA","zip":"20166","mobile":"9033640098","phone":"7035558967","email":"john.dulles@email.com","gender":"MALE","bod":"1981-06-30T04:00:00Z","anv_date":"2011-10-26T04:00:00Z"}{"cust_id":10003,"name":"Albert Lambert","add1":"10000 West OHare","city":"Chicago","state":"Illinois","country":"USA","zip":"60666","mobile":"9724263068","email":"albert.lambert@email.com","gender":"MALE","bod":"1988-07-05T04:00:00Z"}{"cust_id":10004,"name":"Oscar Higgins","add1":"Hangar Center","city":"Brooklyn","state":"New York","country":"USA","zip":"11371","mobile":"8401111085","email":"oscar.higgins@email.com","gender":"MALE","bod":"1990-12-09T05:00:00Z"}{"cust_id":10005,"name":"John Smith","add1":"6000 North Terminal Parkway","city":"Atlanta","state":"Georgia","country":"USA","zip":"30320","mobile":"9574323013","email":"john.smith@email.com","gender":"MALE","bod":"1985-09-24T04:00:00Z"}

0 个答案:

没有答案