0

我试图使这个 ajax 请求函数工作,但 netbeans 发出警告,以下函数并不总是返回值。任何人都可以请帮忙。

function fpform(){
    var response='';
    var fpemail = $('#frgtpwd').val();
    //var fpemail = document.getElementById('frgtpwd').value;

    if (fpemail == ""){
        $('span#fperror').text("insert your emal address");
        //document.getElementById('fperror').innerHTML = "Insert your email address";
        return false;
    } else {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(fpemail)==false) { 
            $('span#fperror').text("Email address is not in valid format");
            //document.getElementById('fperror').innerHTML = "Email address is not in valid format";
            return false;
        } else {
            $("#loader").html('<img src="images/ajax-loader.gif" />');
            $.post("forgot_password_process.php", {
                email:fpemail
            }, function(response){
                response = response.trim();
            }).success(function () {
                if (response == 'yes'){
                    $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    return true;
                } else {
                    alert("your email address was not found");
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
                    return false;
                } 
            });
        }
    }
}
4

2 回答 2

1

您的代码中的return true;语句不是从fpform. 相反,它从给定的回调函数返回.success()。到执行此函数时,外部函数 ,fpform早已返回。使用 ajax 从函数“返回”的唯一方法是使用回调。


在我给你任何代码之前,你已经犯了很多其他错误:

  1. 您的电子邮件正则表达式 ,/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/在我的电子邮件地址上失败。+也是一个有效的字符。考虑不使用 regex 验证电子邮件地址

  2. $("#loader").hide('<img src="images/ajax-loader.gif" />')不起作用。完全没有。你要$("#loader").empty()

  3. 您在顶部声明的变量response被您response在其中一个匿名函数中的参数所遮蔽,因此response = response.trim()没有任何效果。


function fpform(callback) {
    var fpemail = $('#frgtpwd').val();

    if (fpemail == ""){
        $('span#fperror').text("insert your email address");
        callback(false);
    } else {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(fpemail)==false) { 
            $('span#fperror').text("Email address is not in valid format");
            callback(false);
        } else {
            $("#loader").html('<img src="images/ajax-loader.gif" />');
            $.post("forgot_password_process.php", {
                email:fpemail
            }).success(function(response) {
                response = response.trim();
                if (response == 'yes'){
                    $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    callback(true);
                } else {
                    alert("your email address was not found");
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
                    callback(false);
                } 
            }).error(function() { callback(false); });
        }
    }
}
于 2012-09-02T17:50:11.450 回答
0

您应该在之后返回值$.post(...).success(...);

于 2012-09-02T17:52:39.380 回答