2013-06-23 2 views
0

데이터 저장소로 WebSQL을 사용하는 작은 응용 프로그램이 있습니다.
이 데이터를 웹 서버 (PHP + MySQL)와 동기화하고 싶습니다.
WebSQL에서 JSON을 어떻게 전송할 수 있는지 잘 모르는 주된 문제점은 무엇입니까?JSON fton WebSQL을 얻는 방법

//view the result from DB on a web-page 
    $('body').html('<ul id="dataAllHere"></ul>') 
    mybase.init.getAll = function(){ 
      var database = mybase.init.db; 
      database.transaction(function(tx){ 
        tx.executeSql("SELECT * FROM table", [], function(tx,result){ 
         for (var i=0; i < result.rows.length; i++) { 
           item = result.rows.item(i).item; 
           due_date = result.rows.item(i).due_date; 
           the_type = result.rows.item(i).the_type; 
           id = result.rows.item(i).ID; 
           showAll(item,due_date, the_type, id); 
         } 
        }); 
      }); 
    } 

    function showAll(item,due_date, the_type, id){ 
      $('#dataAllHere').append('<li>'+item+' '+due_date+' '+the_type+' '+id+'</li>'); 
    } 
    mybase.init.getAll(); 

저는 JSON에 익숙하지 않아 도움이나 조언을 듣게되어 기쁩니다.
감사합니다.

답변

0

기본적으로 객체/배열을 만들고 json으로 인코딩합니다.이 코드는 동일하지만 모양이 문자열입니다. 코드의 경우 : 그냥 JSON으로 결과에서 모든 것을 밀어 해야하는 경우

var myJson = []; 
for (var i=0; i < result.rows.length; i++) { 
    item = result.rows.item(i).item; 
    due_date = result.rows.item(i).due_date; 
    the_type = result.rows.item(i).the_type; 
    id = result.rows.item(i).ID; 
    showAll(item,due_date, the_type, id); 

    myJson.push({item: item, due_date: due_date, the_type: the_type, id: id}); 
} 

$.ajax({ 
    method: 'post', 
    data: myJson, 
    type: 'json', 
    url: 'target.php' 
}) 
+0

TYWM! 이상하지만, myJson이 정의되지 않았 음을 알려줍니다. –

-1

당신은 루프를 단순화 할 수 있습니다.

for (var i=0; i < result.rows.length; i++) { 
    myJson.push(result.rows.item(i)); 
} 
+0

[기타 답변] (http://stackoverflow.com/a/17262551/2006429)에 대한 코멘트 여야합니다. – usandfriends