简体   繁体   English

函数不总是返回值吗?

[英]function does not always return a value?

I am trying to make this ajax request function work but netbeans is giving a warning that the following function does not always return a value. 我试图使这个ajax请求函数起作用,但是netbeans发出警告,以下函数并不总是返回值。 Can anyone please help. 谁能帮忙。

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;
                } 
            });
        }
    }
}

The return true; return true; statement in your code is not returning from fpform . 代码中的语句不是从fpform返回。 It is instead returning from the callback function given to .success() . 相反,它是从提供给.success()的回调函数返回的。 By the time this function is executed, the outer function, fpform , has long since returned. 到执行此函数时,外部函数fpform早已返回。 The only way to "return" from a function using ajax is with a callback. 使用ajax从函数“返回”的唯一方法是使用回调。


Before I give you any code, you've made a bunch of other mistakes: 在给您任何代码之前,您已经犯了很多其他错误:

  1. Your email regex, /^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$/ , fails on my email address. 您的电子邮件正则表达式/^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$/ ,我的电子邮件地址失败。 + is a valid character as well. +也是有效字符。 Consider not validating email addresses with regex . 考虑不使用regex验证电子邮件地址

  2. $("#loader").hide('<img src="images/ajax-loader.gif" />') does not work. $("#loader").hide('<img src="images/ajax-loader.gif" />')不起作用。 At all. 完全没有 You want $("#loader").empty() 您想要$("#loader").empty()

  3. The variable response you declare at the top is shadowed by your argument response in one of your anonymous functions, making response = response.trim() have no effect whatsoever. 您在顶部声明的变量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); });
        }
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM