Javascript 如何从WebSQL获取JSON
我有一个小应用程序,它使用WebSQL存储数据。 我想将此数据与web服务器PHP+MySQL同步 我的主要问题是,我不知道如何从WebSQL创建JSON来传输它Javascript 如何从WebSQL获取JSON,javascript,jquery,json,web-sql,Javascript,Jquery,Json,Web Sql,我有一个小应用程序,它使用WebSQL存储数据。 我想将此数据与web服务器PHP+MySQL同步 我的主要问题是,我不知道如何从WebSQL创建JSON来传输它 //view the result from DB on a web-page $('body').html('<ul id="dataAllHere"></ul>') mybase.init.getAll = function(){ var database = myb
//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不太熟悉,我很乐意得到任何帮助和建议。基本上,您可以创建一个对象/数组,并将其编码为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'
})
如果需要,可以简化循环 只需将结果中的所有内容都推送到json即可
for (var i=0; i < result.rows.length; i++) {
myJson.push(result.rows.item(i));
}
天哪!奇怪,但它告诉我myJson是未定义的。这应该是对的注释。