简体   繁体   中英

To prevent postback failed: isValid=“undefined”

I have the code to prevent postback but failed. Basically I have an asp.net button.

<asp:Button ID="btnSave" runat="server" Text="SaveChanges" OnClick="btnSave_Click"
        CssClass="saveButton" ValidationGroup="answer" OnClientClick="return ValidateUserNameBeforeSubmitting();" />

And ajax call web service.

function ValidateUserName() {
        $.ajax({ type: "POST",
            url: "../UserNameWebService.asmx/ValidateUserName",
            data: "{'strUsername': '" +JSON.stringify( $("#<%=TextUserName.ClientID%>").val()) + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            async: false,
            success: function (data) {
                return data.d;
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });
    }


    function ValidateUserNameBeforeSubmitting() {
        var isValid = ValidateUserName();
        return isValid;
    }

The web service will return a boolean value and it does when I step into the code. However when I stepped into the javascript code, I found that "isValid" is not a boolean value. It is "undefined". Why?

Thanks.

Ajax is asynchronous.

var isValid = ValidateUserName();

this line executes, but function you're calling has no return (hence undefined )

if you want to access a variable returned from ajax, it needs to be in the success handler.

function ValidateUserName() {
    var returnValue;
    $.ajax({ type: "POST",
        ...
        async: false,
        success: function (data) {
            returnValue = data.d;
        },
        ...
    });
    return returnValue;
}

isValid is undefined because ValidateUserName() doesn't actually return anything.

Change the ajax call to this

 function ValidateUserName() {

        var results = $.ajax({ type: "POST",
        url: "../UserNameWebService.asmx/ValidateUserName",
        data: "{'strUsername': '" +JSON.stringify( $("#<%=TextUserName.ClientID%>").val()) + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        async: false
    });

    return results;
    //or results.d; if thats what you need for the boolean
}

When the ajax is marked as async:false the result of the ajax call contains your result. Rather than it being passed to a success function

Scope a return variable so all paths return and make sure your ajax call return a bool:

function ValidateUserName() {
    var result = false; //default 
    $.ajax({ type: "POST",
        url: "../UserNameWebService.asmx/ValidateUserName",
        data: "{'strUsername': '" +JSON.stringify( $("#  <%=TextUserName.ClientID%>").val()) + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            result = data.d;  //make sure this is a bool
            //result = Boolean(data.d); //use this if returning a string, not recommended though
            alert(result); //are you a bool?
        },
        error: function (xhr, ajaxOptions, thrownError) {              
            alert(xhr.status);
            alert(thrownError);
        }
    });

    return result;
}


function ValidateUserNameBeforeSubmitting() {
    var isValid = ValidateUserName();
    return isValid;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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