javascript - AJAX not firing before Callback -
i'm attempting run ajax query , insert results local database before moving onto new window.location. however, success function insert results not run. instead callback function causes new page load. if comment out if (callback {callback();} ajax completes inserts, window.location code not called. callback function seems called before ajax success function completed. how ajax success insert results before moving on? here's code i'm using:
<script> var db = window.opendatabase("mantismobiledb1", "", "mantismobiledb", 1024 * 1000); function createdb() { db.transaction(function (tx) { tx.executesql('create table if not exists routes(id integer primary key, routeid text, customerid text, stopseq text, driverid text)', []); }); } function insertroute(routeid, customerid, stopseq, driverid) { db.transaction(function (tx) { tx.executesql('insert routes (routeid, customerid, stopseq, driverid) values (?, ?, ?, ?)', [routeid, customerid, stopseq, driverid], countreturns); }); } function loadinitialroutelist(callback) { var dataobject = { postdesignator: 'routes' }; $.ajax({ url: 'http://url.php', data: dataobject, datatype: 'json', type: 'post', success: function (result) { (var = 0, len = result.records.length; < len; ++i) { var route = result.records[i].record; insertroute(route.routeid, null, null, null); } if (callback) { callback(); } } }); } if (localstorage.getitem('exitsatus') === null) { createdb(); loadinitialroutelist(function () { window.location = 'application.html'; }); } else { window.location = localstorage.getitem('exitsatus'); } </script>
Comments
Post a Comment