本文介绍了jQuery - 等待函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数sendMail(from,to,subject,message)

我有一个用于发送带有ajax请求的电子邮件的jquery函数; {
var datastr =from =+ from +& to =+ to +& subject =+ subject +& message =+ message;
$ .ajax({
type:POST,
url:mail.php,
data:datastr,
cache:false,
成功:函数(html){
返回true;
},
错误:函数(jqXHR,状态,错误){
返回false;
}
});
}

现在我想告诉用户邮件是否已成功发送或不需要这:

  $(#feedback)。html(Sending email ...); 
if(sendMail([email protected][email protected],MySubject,MyMessage))
$(#feedback)。html(Email sent 。);
else
$(#feedback)。html(Error sending email。);

但是当然jQuery会在发送邮件之前处理if条件,所以条件为false: - (
)如何让jQuery等到sendMail完成并返回一些内容?

解决方案

虽然给出的答案是有效的,我会考虑保持 sendmail 函数和DOM修改分开(即保持 $(#feedback)。html ...) code> out of the success / error callbacks)。你可以通过返回 ajax call:

  function sendMail(from,to,subject,message){
var datastr =from = + from +& to =+ to +& subject =+ subject +& message =+ message;
return $ .ajax({
type:POST,
url:mail.php,
data:datastr,
cache:false
});
}

返回值,所以你可以这样做:

  $(#feedback) .html(发送电子邮件...); 
sendMail([email protected][email protected],MySubject,MyMessage)
.done(function(){
$(#feedback ).html(Email sent。);
})
.fail(function(){
$(#feedback)。html(Error sending email。) ;
});

请注意,我删除了成功 error 来自ajax调用的字段,因为它们不是必需的,但如果您需要它们用于其他事情(如日志记录),您仍然可以使用它们。


I have a jquery function for sending email with ajax request;

function sendMail(from,to,subject,message){
    var datastr="from="+from+"&to="+to+"&subject="+subject+"&message="+message;
    $.ajax({
        type: "POST",
        url: "mail.php",
        data: datastr,
        cache: false,
        success: function(html){
            return true;
        },
        error: function(jqXHR,status,error){
            return false;
        }
    });
}

now I want to tell the user if the mail was successfully sent or not like this:

$("#feedback").html("Sending email...");
if(sendMail("[email protected]","[email protected]","MySubject","MyMessage"))
   $("#feedback").html("Email sent.");
else
   $("#feedback").html("Error sending email.");

but of course jQuery processes the if condition before the mail was sent, so the condition is false :-(How do I tell jQuery to wait until sendMail has completed and returns something?

解决方案

While the given answers are valid, I would consider keeping the sendmail function and the DOM modifications separate (i.e. keeping $("#feedback").html... out of the success/error callbacks ). You can do this by returning the result of the ajax call:

function sendMail(from,to,subject,message){
    var datastr="from="+from+"&to="+to+"&subject="+subject+"&message="+message;
    return $.ajax({
        type: "POST",
        url: "mail.php",
        data: datastr,
        cache: false
    });
}

The return value implements the Promise interface, so you can then do this:

$("#feedback").html("Sending email...");
sendMail("[email protected]","[email protected]","MySubject","MyMessage")
    .done(function(){
        $("#feedback").html("Email sent.");
    })
    .fail(function(){
        $("#feedback").html("Error sending email.");
    });

Note that I removed the success and error fields from the ajax call since they're not needed, but you could still use them if you need them for something else, like logging.

这篇关于jQuery - 等待函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:44