我正在尝试运行AJAX查询并将结果插入本地数据库,然后再转到新的window.location。但是,插入结果的成功函数不会运行。相反,回调函数会导致新页面加载。如果我注释掉if(callback {callback();}那么AJAX完成插入,但是window.location代码显然没有被调用。回调函数似乎在AJAX成功函数完成之前被调用。我该怎么办在继续之前获得AJAX成功插入结果?这是我正在使用的代码:
<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 INTO 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) {
for (var i = 0, len = Result.records.length; i < 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>