成功提交JQuery Form $ .post()句柄显示消息

时间:2014-11-17 19:44:16

标签: javascript php jquery

我有一个我在jquery中提交$ .post()的表单。

$.post("testpage.php", $("#payment-form").serialize())

帖子本身很好用,但是当帖子完成并成功后,页面会做其他的事情,比如显示谢谢你的消息和内容。我只需要在哪里打电话来显示消息。我不明白它从帖子返回的成功方面。

3 个答案:

答案 0 :(得分:2)

你可以指定.post()的第三个参数,即"成功"打回来。这是.post执行成功时调用的函数。

$.post("testpage.php", $("#payment-form").serialize(), function() { alert('post was successful!')})

来源:http://api.jquery.com/jquery.post/

答案 1 :(得分:2)

简而言之,就像这样:

$.post("testpage.php", $("#payment-form").serialize(), function () {
    // Start partying here.
}).fail(function() {
    // Handle the bad news here.
})

答案 2 :(得分:2)

或者,您可以使用deferred objects,如下所示:

// Create POST request
var paymentPost = $.post("testpage.php", $("#payment-form").serialize());

// Assigned deferred objects
// "data" refers to the data, preferably in JSON format, returned by testpage.php
paymentPost
.done(function(data) {
    // Success
    // e.g. display thank you message, redirect to a payment successful page...
})
.fail(function(data) {
    // If error
    // e.g. display error message(s)
})
.always(function() {
    // Will always fire as long as POST request is submitted and completed
});

p / s:重要的是要注意不推荐使用.success().error()等jqXHR方法。为了准备最终删除,你应该遵守延迟对象的新命名法;)

相关问题