2013-04-07 2 views
0

현재 웹 SQL 행을 처리하는 데 다음 코드가 있습니다. 현재는 모든 행에 대해 서버에 요청을 보냅니다. 어떻게 든 모든 행을 다차원 JSON 객체에 병합하고 싶습니다. 그러나이를 수행하는 방법에 대한 문서를 찾을 수없는 것 같습니다.JSON 문자열을 병합하여 AJAX를 간소화하십시오.

결과를 반복하면서 개체에 "추가"할 수있는 방법이 있습니까?

$("#sync-surveys").click(function(){ 
$.mobile.showPageLoadingMsg(); 
db.transaction(function(tx) { 
    tx.executeSql("SELECT * FROM surveys", [], function(tx, result) {   
    for (var i = 0, item = null; i < result.rows.length; i++) { 
     item = result.rows.item(i); 
     var json_str = JSON.stringify(item); 

     $.ajax({ 
      type: "post", url: "/survey/survey/process_survey", 
      data: json_str, 
      success: function (data) { 
       alert("saved successfully"); 
       $.mobile.hidePageLoadingMsg(); 
      }, 
      error: function (data) { 
       alert(data.responseText); 
       $.mobile.hidePageLoadingMsg(); 
       //console.log(data); 
      } 
     });//End ajax function 
    }//End results loop 


    }); 
});//End Transaction 

});//End Surveys Click 
+0

http://api.jquery.com/jQuery.extend/ – adeneo

답변

0

"문자열을 병합하지 마십시오." 대신 "그래프 작성".

// build data-structure/object graph - alter as required 
// make sure to choose something easy to consume/extend. 
var items = []; 
for (var i = 0; i < result.rows.length; i++) { 
     var item = result.rows.item(i); 
     // maybe do something else here? if not, it might be possible 
     // to JSON.stringify result.rows directly and/or use Array.slice 
     // to avoid this loop entirely 
     items.push(item); 
} 
// send request - only build JSON from JS object graph once at end 
// JSON = text: modify graph before converting for transmission 
$.ajax({ 
    data: JSON.stringify(items), 
    .. 
}); 
+0

완벽하게 작동합니다. – Adam

관련 문제