我想在完成运行“saveToFile()”函数后运行第一个ajax调用,然后在完成后运行第二个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"},
});
}
});
答案 0 :(得分: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
});
}
答案 1 :(得分:1)
适合我:http://jsbin.com/upujur/2/edit
问题可能在于此行中的点而不是逗号:
type: "POST".
您使用的是像FireBug或Developer Tools这样的JavaScript控制台来调试程序吗?