包含JQuery的JavaScript函数ajax - 函数返回undefined [英] Javascript function containing JQuery ajax - Function returns undefined

查看:88
本文介绍了包含JQuery的JavaScript函数ajax - 函数返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以给我一个线索,为什么这不起作用?该函数返回undefined。它警告一个布尔值,但仍然返回undefined?!



谢谢

  function IsUniqueEmail(){

var email = $(#<%= EmailAddress.ClientID%>)。val();

if(email.length> 0){

$ .ajax({
url:'handlers / validator.ashx',
dataType :'json',
data:{n:email,v:email},
async:false,
success:function(data){
alert(eval(data.success));
返回eval(data.success);
},
错误:函数(XMLHttpRequest,textStatus,errorThrown){
console.log (textStatus,errorThrown);
返回true;
}
});



$(document).ready(function(){

var execScript = $(。buttonStep1)。attr( (.href,#)。click(function(){$(.href,#)。 b $ b //在这里点击逻辑前添加
var IsOk = IsUniqueEmail();
if(IsOk){
$(#EmailAddressInUseMessage)。hide();
eval(execScript);
}
else {
$(#EmailAddressInUseMessage)。show();
}
});
}) ;

这是ajax调用的响应

  {success:false,error:ERROR_EMAILINUSE} 


解决方案

使用AJAX时的脚本执行路径不是线性的。您的 IsUniqueEmail 会在AJAX请求收到响应之前退出,不会返回任何内容。被发送到 $。ajax ,这是调用 success 的方法,在那里它可能被忽略。



您可以用这种方式重写代码:

  $(文件).ready(函数(){
$(。buttonStep1)。click(function(){

IsUniqueEmail();

//永远不会请点击此链接
返回false;

});
});

通过这种方式,按钮点击只是开始请求 - 它不会继续,其他。在这之后,你在AJAX响应中做了所有的魔法:

 函数IsUniqueEmail(){

var email = $(#<%= EmailAddress.ClientID%>)。val();

if(email.length> 0){

$ .ajax({
url:'handlers / validator.ashx',
dataType :'json',
data:{n:email,v:email},
async:false,
success:function(data){
alert(eval(data.success));

if(eval(data.success)){

//点击这里执行代码以继续



错误:函数(XMLHttpRequest,textStatus,errorThrown){
console.log(textStatus,errorThrown);
返回true;
}
});
}
}


Can any give me a clue why this isn't working? The function returns undefined. It alerts a boolean values but still return undefined?!

Thanks

    function IsUniqueEmail() {

        var email = $("#<%=EmailAddress.ClientID%>").val();

        if (email.length > 0) {

            $.ajax({
                url: 'handlers/validator.ashx',
                dataType: 'json',
                data: { "n": "email", "v": email },
                async: false,
                success: function(data) {
                    alert(eval(data.success));
                    return eval(data.success);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log(textStatus, errorThrown);
                    return true;
                }
            });
        }            
    }

    $(document).ready(function() {

        var execScript = $(".buttonStep1").attr("href").replace("javascript:", "");

        $(".buttonStep1").attr("href", "#").click(function() {
            // Add before click logic here
            var IsOk = IsUniqueEmail();
            if (IsOk) {
                $("#EmailAddressInUseMessage").hide();
                eval(execScript);
            }
            else {
                $("#EmailAddressInUseMessage").show();
            }
        });
    });

this is the response of the ajax call

    { "success": false, "error" : "ERROR_EMAILINUSE" }

解决方案

The script execution path when using AJAX is not linear. Your IsUniqueEmail will exit before the AJAX request has received a response, returning nothing.

What you're returning is being sent to $.ajax, which is the method that is invoking success, and there it is probably being disregarded.

You could rewrite your code in this manner:

$(document).ready(function() {
    $(".buttonStep1").click(function() {

        IsUniqueEmail();

        // never follow this link
        return false;

    });
});

In this way, the button click just starts the request - it doesn't go ahead and do anything else. After that, you do all your magic in the AJAX response:

function IsUniqueEmail() {

    var email = $("#<%=EmailAddress.ClientID%>").val();

    if (email.length > 0) {

        $.ajax({
            url: 'handlers/validator.ashx',
            dataType: 'json',
            data: { "n": "email", "v": email },
            async: false,
            success: function(data) {
                alert(eval(data.success));

                if(eval(data.success)) {

                   // Execute code to continue after the click here

                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus, errorThrown);
                return true;
            }
        });
    }            
}

这篇关于包含JQuery的JavaScript函数ajax - 函数返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆