My PHP file doing 2 operations: 1. Submits data from form into db table, 2. Sends email. What I wanna do is to show status messages via ajax. For ex: "First operation done, please wait for second" and then when second one will be finished show the next message "Second op. done too". Now my ajax looks like that. How can I modify it?
我的PHP文件执行2个操作:1。将表单中的数据提交到db表,2。发送电子邮件。我想做的是通过ajax显示状态消息。例如:“首先完成操作,请等待第二个”然后当第二个操作完成时显示下一个消息“第二个操作也完成”。现在我的ajax看起来像那样。我怎么修改它?
//add status data to form
form.data('formstatus', 'submitting');
if (validate()) {
//send data to server for validation
$.ajax({
url: formUrl,
type: formMethod,
dataType: "json",
data: formData,
success: function (data) {
$.notifyBar({
cls: data.status,
html: data.message
});
form.data('formstatus', 'idle');
}
});
}
3 个解决方案
#1
3
in the success block you can perform another ajax call. That's the simplest. You can do it to in .success(), .ajaxSucces(), .complete(), or .then() function like this: $.ajax(...).success(...);
在成功块中,您可以执行另一个ajax调用。那是最简单的。您可以在.success(),. ajaxSucces(),. complete()或.then()函数中执行此操作:$ .ajax(...)。success(...);
ideally you would embed the code in a function, by example
理想情况下,您可以通过示例将代码嵌入到函数中
$.ajax({
url: formUrl,
type: formMethod,
dataType: "json",
data: formData,
success: function (data) {
notifyResponse(data);
form.data('formstatus', 'idle');
sendMail();
}
});
function sendMail() {
$.get(mailUrl, function(data) { // or $.post(...
notifyResponse(data);
});
}
function notifyResponse(data) {
$.notifyBar({
cls: data.status,
html: data.message
});
}
#2
3
If you've to do two operations that have different execution times, just send two different AJAX queries, and get the responses from them.
如果您要执行两个具有不同执行时间的操作,只需发送两个不同的AJAX查询,并从中获取响应。
Divide your PHP service in two parts. If the second part depends on the first, instead of sending the two requests at the same time, send the second request when the first one returns.
将PHP服务分为两部分。如果第二部分依赖于第一部分,而不是同时发送两个请求,则在第一部分返回时发送第二个请求。
In other words, in your success callback, you're going to notify the user that the first operation has been completed and you proceed to call the second operation, whose success callback will inform that the second operation has been completed.
换句话说,在成功回调中,您将通知用户第一个操作已完成,然后继续调用第二个操作,其成功回调将通知第二个操作已完成。
#3
0
Another approach different from the one suggested by stivlo is to use a sort of long polling like the one explained in this answer:
另一种不同于stivlo建议的方法是使用一种类似于这个答案中解释的长轮询:
How do I implement basic "Long Polling"?
如何实施基本的“长轮询”?
Not so simple and with more technical problem abaout server configuration.
不是那么简单,而且在服务器配置上存在更多技术问题。
#1
3
in the success block you can perform another ajax call. That's the simplest. You can do it to in .success(), .ajaxSucces(), .complete(), or .then() function like this: $.ajax(...).success(...);
在成功块中,您可以执行另一个ajax调用。那是最简单的。您可以在.success(),. ajaxSucces(),. complete()或.then()函数中执行此操作:$ .ajax(...)。success(...);
ideally you would embed the code in a function, by example
理想情况下,您可以通过示例将代码嵌入到函数中
$.ajax({
url: formUrl,
type: formMethod,
dataType: "json",
data: formData,
success: function (data) {
notifyResponse(data);
form.data('formstatus', 'idle');
sendMail();
}
});
function sendMail() {
$.get(mailUrl, function(data) { // or $.post(...
notifyResponse(data);
});
}
function notifyResponse(data) {
$.notifyBar({
cls: data.status,
html: data.message
});
}
#2
3
If you've to do two operations that have different execution times, just send two different AJAX queries, and get the responses from them.
如果您要执行两个具有不同执行时间的操作,只需发送两个不同的AJAX查询,并从中获取响应。
Divide your PHP service in two parts. If the second part depends on the first, instead of sending the two requests at the same time, send the second request when the first one returns.
将PHP服务分为两部分。如果第二部分依赖于第一部分,而不是同时发送两个请求,则在第一部分返回时发送第二个请求。
In other words, in your success callback, you're going to notify the user that the first operation has been completed and you proceed to call the second operation, whose success callback will inform that the second operation has been completed.
换句话说,在成功回调中,您将通知用户第一个操作已完成,然后继续调用第二个操作,其成功回调将通知第二个操作已完成。
#3
0
Another approach different from the one suggested by stivlo is to use a sort of long polling like the one explained in this answer:
另一种不同于stivlo建议的方法是使用一种类似于这个答案中解释的长轮询:
How do I implement basic "Long Polling"?
如何实施基本的“长轮询”?
Not so simple and with more technical problem abaout server configuration.
不是那么简单,而且在服务器配置上存在更多技术问题。