通过AJAX提交多种形式 - 同步或异步 [英] Submitting multiple forms via AJAX - sync or async
问题描述
我有一点的情况下,我有我需要通过AJAX由$ P $将同一页面上的多个表单pssing一个保存按钮。我用$。每次通过循环的形式,然后使Ajax请求这样做。 Ajax请求都应该发回一个响应(错误/成功消息)。
I have a bit of situation where I have multiple forms on the same page that I need submit via ajax by pressing a single "Save" button. I'm doing this by looping through the forms using $.each and then making the ajax request. The ajax requests are supposed to send back a response (errors / success message).
我的问题是 - 我会使用异步请求中犯了一个错误,因为可能有与之相配的一个问题,它输出到何种形式?你们有什么感想?
My problem is - would I be making a mistake by using ASYNC requests, as there might be a problem with matching up which output goes to which form ? What do you guys think ?
推荐答案
不要使用同步XHR请求;有使用它们没有任何正当理由,而且会导致可怕的用户体验。
Never use synchronous XHR requests; there's no valid reason to use them, and it results in terrible user experience.
您不必担心会混在一起的要求:
You don't have to worry about the requests getting mixed up:
$('form').each(function() {
var form = $(this);
$.post(form.attr('action'), form.serialize(), function(r) {
// `form` is still the particular form submitted,
// and `r` will be the results of posting that form.
});
});
这篇关于通过AJAX提交多种形式 - 同步或异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!