0

我想在第一个 ajax 调用完成后运行函数“saveToFile()”,然后在完成后运行第二个 ajax 调用。似乎不像编码那样工作。我对 ajax 还很陌生,所以在这方面有点挣扎。

// save composition to database 
$.ajax({ 
    type: "POST", 
    url: "_inc/save_to_db.php", 
    data: {jsonStr: canvasStr}, 
    success: function(data) {
            // this saves the comp to the _comps folder on the server as a jpeg file (firth.js)
            saveToFile();

            $.ajax({
            type: "POST".
            url: "_inc/generate_thumbnail.php",
            data: {imgFile: "<?php echo $_SESSION['UserID'] ?>_<?php echo $_SESSION['CompID'] ?>.jpg"},
            });
        }
    });
4

2 回答 2

1

您可以使saveToFile函数将回调作为参数,一旦它执行的 AJAX 调用完成,该回调将被执行。例如:

$.ajax({ 
    type: "POST", 
    url: "_inc/save_to_db.php", 
    data: {jsonStr: canvasStr}, 
    success: function(data) {
        // this saves the comp to the _comps folder on the server as a jpeg file (firth.js)
        saveToFile(function(result) {
            $.ajax({
                type: "POST",
                url: "_inc/generate_thumbnail.php",
                data: {imgFile: "<?php echo $_SESSION['UserID'] ?>_<?php echo $_SESSION['CompID'] ?>.jpg"},
            });
        });
    }
});

你的saveToFile函数现在可能看起来像这样:

function saveToFile(successCallback) {
    $.ajax({
        url: ...,
        type: ...,
        data: ...,
        success: successCallback
    });
}
于 2013-06-17T06:34:33.350 回答
1

为我工作:http: //jsbin.com/upujur/2/edit

问题可能出在这一行中的点而不是逗号:

type: "POST".

您是否使用诸如 FireBug 或开发者工具之类的 JavaScript 控制台来调试您的程序?

于 2013-06-17T06:46:49.003 回答