简体   繁体   English

成功提交JQuery表单$ .post()处理显示消息

[英]Submit JQuery Form $.post() handle display message on success

I have a form I am submitting with $.post() in jquery.. 我有一个表单要用$ .post()在jquery中提交。

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

The post works fine itself, but when the post is done and successful the page will do other stuff such as display a thank you message and stuff. 该帖子本身可以正常工作,但是完成该帖子并获得成功后,该页面将执行其他操作,例如显示感谢消息和其他内容。 I just need to where to put to call to display the message.I do not understand where it goes in terms of the success being returned from the post. 我只需要在哪里打电话就可以显示消息。我不明白从邮件中返回成功消息的意义。

You can specify a 3rd argument to .post() that is the "success" callback. 您可以为.post()指定第三个参数,即“成功”回调。 It is a function to call when the .post execution is successful. .post执行成功时,将调用此函数。

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

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

In short, like this: 简而言之,像这样:

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

Alternatively, you can use deferred objects , like as follow: 另外,您可以使用延迟对象 ,如下所示:

// 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: It is important to note that jqXHR methods like .success() and .error() are deprecated. p / s:请注意,不建议使用诸如.success().error()类的.success()方法,这一点很重要。 To prepare for their eventual removal, you should abide to the new nomenclature for deferred objects ;) 为最终将其移除做准备,您应该遵守有关延迟对象的新术语;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM