Показывать сообщение до получения ответа ajax

Я хочу показать сообщение об ошибке, пока не придет ответ ajax. Попробовал с помощью следующего кода:

$.ajax({
url: "user.php",
type: 'POST',
data: $('#forgot-form').serialize(),
beforeSend: function(){
$('#error').html("Please wait until your request is processed.");
setTimeout(function() { $("#error").html(''); }, 8000);
}
} ).done (function(data) {
/* I tried with the commented code also. But this section didn't worked */
//$('#error').html("Please wait until your request is processed.");
// setTimeout(function() { $("#error").html('Please wait until your request is processed.'); }, 8000);
if (data == "success") {
$('#user-login').val('');
$('#error').html("Please check your email to reset your password");
setTimeout(function() { $("#error").html(''); }, 5000);
}  else if (data == "invalid") {

$('#error').html("Username or email doesnot exist");
setTimeout(function() { $("#error").html(''); }, 5000);
$('#user-login').focus();
return false;
}
});

В user.php страница, я отправляю электронное письмо пользователю, если данные пользователя верны. Так что ответ приходит только поздно. До этого времени я хочу показать ожидающее сообщение.

Этот код показывает ожидающее сообщение правильно. Но после этого происходит небольшая задержка, после чего отображается только сообщение об успехе. Мне нужно избегать этой задержки. Как я могу изменить код, чтобы избежать задержки?

Может ли кто-нибудь помочь мне сделать это? Заранее спасибо.

0

Решение

Попробуй это:

Просто удалите setTimeout() от перед отправкой перезвонить. Он покажет сообщение в #error и заменит сообщение после завершения ajax.

 $.ajax({
url: "user.php",
type: 'POST',
data: $('#forgot-form').serialize(),
beforeSend: function(){
$('#error').html("Please wait until your request is processed.");

}
} ).done (function(data) {
/* I tried with the commented code also. But this section didn't worked */
//$('#error').html("Please wait until your request is processed.");
// setTimeout(function() { $("#error").html('Please wait until your request is processed.'); }, 8000);
if (data == "success") {
$('#user-login').val('');
$('#error').html("Please check your email to reset your password");
setTimeout(function() { $("#error").html(''); }, 5000);
}
else if (data == "invalid") {

$('#error').html("Username or email doesnot exist");
setTimeout(function() { $("#error").html(''); }, 5000);
$('#user-login').focus();
return false;
}
});
1

Другие решения

Удалить свой beforeSend() и установить ожидающее сообщение перед $.ajax вызов.

 $('#error').html("Please wait until your request is processed.");
$.ajax({
url: "user.php",
type: 'POST',
data: $('#forgot-form').serialize(),
}).done (function(data) {
if (data == "success") {
$('#user-login').val('');
$('#error').html("Please check your email to reset your password");
setTimeout(function() { $("#error").html(''); }, 5000);
}  else if (data == "invalid") {
$('#error').html("Username or email doesnot exist");
setTimeout(function() { $("#error").html(''); }, 5000);
$('#user-login').focus();
return false;
}
});
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector