jQuery ajax 完成后调用执行函数

标签 jquery ajax

我正在开发一个移动应用程序。我使用 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/

相关文章:

jquery - 如何在触摸 Canvas html5时设置图像

javascript - 将数据从 HTML 传递到 JQuery/Ajax

javascript - Ajax base64 字符串无法成功工作

javascript - 为什么 Chrome 中的 mailto 链接与 POST 请求冲突?

Jquery移动: Binding on "change" not excuted after redirect (rails 4)

jQuery 对象属性而不是类

javascript - 为什么这个脚本在 IE8 中不起作用?

javascript - jqgrid,在编辑表单中填充值的按钮?

ruby-on-rails - Ajax 请求不适用于 Rails 中的 Controller 操作

javascript - Jquery 相关选择