Android phonegap$.ajax()

Android phonegap$.ajax(),android,sql,jquery,cordova,Android,Sql,Jquery,Cordova,有人知道如何将来自服务器的jsonp数据存储在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){ o

有人知道如何将来自服务器的jsonp数据存储在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.');
    }
});
下面的代码有助于将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.');
    }
});

查看HTML5的本地存储

$.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.');
}
});
PhoneGap的it文档:


有关本地数据库的更多信息

请尝试这样做,希望这能奏效:

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');
}

很久以前,我为这类东西创建了一个基本的数据库控制器类,设法找到了它,希望它能给你一个想法

数据库Ctrl
代码放置在某个位置后,您就可以这样使用它:

var myDatabase = DataBaseCtrl();
myDatabase.initWithConfig("DBShortName", "1.0", "MyDbName", 10000);
myDataBase.executeSql("SQL commands here...");
在您的情况下,根据数据的外观设置表

myDataBase.executeSql("CREATE TABLE IF NOT EXISTS LOGS (id unique, log)");
myDataBase.executeSql("INSERT INTO LOGS (id, log) VALUES (1, 'foobar')");
myDataBase.executeSql("INSERT INTO LOGS (id, log) VALUES (2, 'logmsg')");
然后可能使用一个循环来获取所有数据:

for (i = 0; i < data.length; i += 1) {
    myDataBase.executeSql("INSERT INTO LOGS (id, log) VALUES ("+i+", "+data[i]+")");
}
这是类代码:

 var DataBaseCtrl = function () {

    if (!(this instanceof DataBaseCtrl)) {
        return new DataBaseCtrl();
    }

    // Transaction error callback
    function errorCB(tx, err) {
        console.log("Error processing SQL: " + tx + tx.code + tx.message);
    }

    function successCB(tx, err) {
    }

    return {
        _DB: null,
        _config: {
            // Default configuration
            _shortName: "DefaultDataBaseName",
            _version: "1.0",
            _displayName: "DisplayName",
            _maxSize: 65535 // in MBs
        },
        /* Initializer */
        init: function () {
            if (!window.openDatabase) {
                alert("Databases are not supported on this device. \n\n ");
                return false;
            }

            var cfg = {
                shrt: this._config._shortName,
                vers: this._config._version,
                disp: this._config._displayName,
                mxSz: this._config._maxSize
            };
            // Initialize the DataBase.
            this._DB = window.openDatabase(cfg.shrt, cfg.vers, cfg.disp, cfg.mxSz);
        },
        /* Initialize with custom config */
        initWithConfig: function (shortName, version, displayName, maxSize) {
            this.setInitConfig(shortName, version, displayName, maxSize);
            this.init();
        },
        /* Execute SQL command */
        executeSql: function (SqlCmmnd) {
            this._DB.transaction(function (tx) {
                console.log("Executing SQL... " + SqlCmmnd.substring(0, 100));
                tx.executeSql(SqlCmmnd);
            }, errorCB, successCB);
        },
        /* Execute SQL with success callback */
        executeSqlWithCallBack: function (SqlCmmnd, SuccessCallback) {
            this._DB.transaction(function (tx) {
                console.log("Executing SQL... " + SqlCmmnd.substring(0, 100));
                tx.executeSql(SqlCmmnd, [], SuccessCallback);
            }, errorCB, successCB);
        },
        /* Sets init config (call before initializing) */
        setInitConfig: function (shortName, version, displayName, maxSize) {
            console.log("Setting DB Config: " + displayName);
            this._config = {
                _shortName: shortName,
                _version: version,
                _displayName: displayName,
                _maxSize: maxSize
            };
        }
    };
}; 

使用数组存储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表数组,而无需重新导入。

我建议您将对象转换为字符串,然后将其保存在localStorage中

要检索数据,请从localStorage获取字符串并将其转换为JSON对象