1

我正在尝试运行 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>
4

0 回答 0