我正在尝试在成功完成 POST 请求时调用 jQuery GET 请求。这些功能有效,数据从 GET 请求馈送,但是,它在 POST 之前响应。
function getCartCount() {
var d = new Date().getTime();
$.get("/ajax/countCart.php", { "rand": d }, function(res) {
$("#view-cart").text(res);
alert(res);
});
}
$(".products form img").click(function() {
$.post("/ajax/addToCart.php", $(this).parent("form").serialize())
.done(function(data) {
alert(data);
})
.always(getCartCount());
});
上面的代码首先从 GET 请求生成一个警告框,然后从 POST 生成一个警告框,这并不理想,因为 GET 的值取决于先完成的 POST。
最佳答案
.always(getCartCount());
^^
您正在立即调用该函数并将返回值传递给always
。
删除 ()
以传递函数本身。
.always(getCartCount);
关于javascript - 发布后调用 jQuery Get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235546/