0

サーバーからのjsonpデータをphonegapローカルデータベースに保存する方法を知っている人はいますか?

以下のコードは、phonegap android アプリをサーバーに接続するのに役立ちますが、phonegap ローカル データベースにデータを保存する方法を教えてください。

$.ajax({
    url: 'http://172.18.75.156/deals.php',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
    success: function(data, status){
        $.each(data, function(i,item){ 
            output.text('successful');
        });
    },
    error: function(){
       output.text('There was an error loading the data.');
    }
});
4

6 に答える 6

1

これがうまくいくことを願って、このようにしてみてください:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    db = window.openDatabase("SQL", 3, "PhoneGap Demo", 200000);
    db.transaction(ajex_call, success, errorCB);
}
function ajex_call(tx) {
    tx.executeSql('DROP TABLE IF EXISTS table_name');
    tx.executeSql('CREATE TABLE IF NOT EXISTS table_name (fields_required_for_table)');
    $.ajax({ url: 'http://172.18.75.156/deals.php', dataType: 'jsonp', jsonp: 'jsoncallback', timeout: 5000, success: function(data, status){
        $.each(data, function(i,item){
            tx.executeSql("INSERT OR REPLACE INTO table-name(table-fields) values(?,?,..)")
        });
    }, error: function(){
        output.text('There was an error loading the data.');
    } 
}); 
}

function success(){
    console.log('Success');
}
function error(){
    console.log('error');
}
于 2015-08-27T17:53:35.880 に答える
1
  db = window.openDatabase("SQL", 3, "PhoneGap Demo", 200000);

db.transaction(ajex_call, errorCB);

    function ajex_call(tx) {
$.ajax({
url: 'http://172.18.75.156/deals.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
    $.each(data, function(i,item){ 
    //item.obj
        tx.executeSql("INSERT OR REPLACE INTO table-name(table-fields) values(?,?,..)",               [array-data])
    });
       },
error: function(){
   output.text('There was an error loading the data.');
}
});
}

ローカル データベースの詳細情報http://docs.phonegap.com/en/2.2.0/cordova_storage_storage.md.html

于 2012-12-18T09:30:38.633 に答える
0

配列を使用して、JSON インポートからのデータを格納します。次に、アレイをローカル ストレージに保存します。

$.ajax({
url: 'http://172.18.75.156/deals.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
    var ArrayName = []; 
    $.each(data, function(i,item){ 
        output.text('successful');
        ArrayName[i] = item;
    });
    localStorage.setItem("jsontable",ArrayName);
},
error: function(){
   output.text('There was an error loading the data.');
}
});

次に、次を使用してその配列を呼び出すことができますlocalStorage.GetItem("jsontable");

その後、ユーザーは再インポートすることなく、インポートされた json テーブル配列を使用できるようになります。

于 2016-03-09T23:22:19.370 に答える
0

HTML5 のローカル ストレージを確認してください。

PhoneGap のドキュメントはこちら:

于 2012-11-04T02:38:50.880 に答える