0

現在、Web SQL 行を処理する次のコードがあります。現在、すべての行に対してサーバーにリクエストを送信します。どうにかしてすべての行を多次元の JSON オブジェクトにマージしたいのですが、その方法に関するドキュメントが見つからないようです。

結果をループするときに、オブジェクトに「追加」する方法はありますか?

$("#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
4

1 に答える 1

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),
   ..
});
于 2013-04-07T20:07:49.697 に答える