kosbou
kosbou

Reputation: 3999

jQuery ajax call execute function after complete

I am developing a mobile app. I use ajax calls to receive data from a webserver with this code:

$.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;
    }
});

I want the complete function to be executed after all data inserted to SQLite...

How can I do this?

Upvotes: 2

Views: 1994

Answers (1)

Rémi Breton
Rémi Breton

Reputation: 4269

The complete callback should be in your executeSql function. executeSql should accept three arguments:

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

You should edit your executeSql function and make it accept a third argument, and end the function with callback();.

On another note, isn't passing SQL queries like this a security issue? Like, a major one?

Upvotes: 2

Related Questions