我正在开发一个移动应用程序。我使用 ajax 调用通过以下代码从网络服务器接收数据:
$.ajax({
url: 'http://www.xxxxxxxxxxxxxxxx',
data: {
name: 'Chad'
},
dataType: 'jsonp',
success: function(data) {
$.each(data.posts, function(i, post) {
$.mobile.notesdb.transaction(function(t) {
t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?);',
[post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.receiptno],
//$.mobile.changePage('#page3', 'slide', false, true),
null);
});
});
},
complete: function() {
test = 1;
}
});
我希望在将所有数据插入 SQLite 后执行 complete
函数...
我该怎么做?
最佳答案
complete
回调应该位于您的 executeSql
函数中。 executeSql
应该接受三个参数:
var sqlString = 'INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?);';
var sqlValues = [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.receiptno];
var callback = function(){ test = 1; }
executeSql(sqlString, sqlValues, callback);
您应该编辑 executeSql
函数并使其接受第三个参数,并使用 callback();
结束该函数。
另一方面,像这样传递 SQL 查询不是一个安全问题吗?比如,一个主要的?
关于jQuery ajax 完成后调用执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9401435/