最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Calling VB.NET WebMethod Function from Javascript - Stack Overflow

programmeradmin1浏览0评论

I have a VB.NET function which looks like this:

<WebMethod()> _
Public Shared Function AuthenticateUser(ByVal UserInfo As String, ByVal Password As String) As Boolean
    Dim UserName As String

    'Just in case
    AuthenticateUser = False

    'Extract the user name from the user info cookie string
    UserName = Globals.GetValueFromVBCookie("UserName", UserInfo)

    'Now validate the user
    If Globals.ValidateActiveDirectoryLogin("Backoffice", UserName, Password) Then
        AuthenticateUser = True
    End If

End Function

I'm trying to call it from javascript like this:

function DeleteBatchJS()
{if (confirm("Delete the ENTIRE batch and all of its contents? ALL work will be lost."))
     var authenticated = PageMethods.AuthenticateUser(get_cookie("UserInfo"), prompt("Please enter your password"))
     if (authenticated == true)
           {{var pleted = PageMethods.DeleteBatchJSWM(get_cookie("UserInfo"));
            window.location = "BatchOperations.aspx";
            alert("Batch Deleted.");}}}

It calls the function, but won't return a value. When walking through the code, my VB function does fire (it will return true so long as the correct password is typed in), but the javascript 'authenticated' value remains 'undefined'. It's like you can't return values from VB functions to javascript.

I also tried

if PageMethods.AuthenticateUser("UserName", "Password")
   {
     //Stuff
   }

But still no luck.

What am I doing wrong?

Thanks,

Jason

I have a VB.NET function which looks like this:

<WebMethod()> _
Public Shared Function AuthenticateUser(ByVal UserInfo As String, ByVal Password As String) As Boolean
    Dim UserName As String

    'Just in case
    AuthenticateUser = False

    'Extract the user name from the user info cookie string
    UserName = Globals.GetValueFromVBCookie("UserName", UserInfo)

    'Now validate the user
    If Globals.ValidateActiveDirectoryLogin("Backoffice", UserName, Password) Then
        AuthenticateUser = True
    End If

End Function

I'm trying to call it from javascript like this:

function DeleteBatchJS()
{if (confirm("Delete the ENTIRE batch and all of its contents? ALL work will be lost."))
     var authenticated = PageMethods.AuthenticateUser(get_cookie("UserInfo"), prompt("Please enter your password"))
     if (authenticated == true)
           {{var pleted = PageMethods.DeleteBatchJSWM(get_cookie("UserInfo"));
            window.location = "BatchOperations.aspx";
            alert("Batch Deleted.");}}}

It calls the function, but won't return a value. When walking through the code, my VB function does fire (it will return true so long as the correct password is typed in), but the javascript 'authenticated' value remains 'undefined'. It's like you can't return values from VB functions to javascript.

I also tried

if PageMethods.AuthenticateUser("UserName", "Password")
   {
     //Stuff
   }

But still no luck.

What am I doing wrong?

Thanks,

Jason

Share Improve this question edited Jul 12, 2011 at 19:09 Darin Dimitrov 1.0m275 gold badges3.3k silver badges2.9k bronze badges asked Jul 12, 2011 at 18:47 user724198user724198 2
  • side note, I would never prompt for a password like that. – Joel Coehoorn Commented Jul 12, 2011 at 18:53
  • @Joel -- the password coding is there for the sake of simplicity. The real code is a little more involved – user724198 Commented Jul 12, 2011 at 18:54
Add a ment  | 

1 Answer 1

Reset to default 4

Web methods are invoked using AJAX, i.e. asynchronously, i.e. you have to wait until the method pletes before consuming the results, i.e. you have to use the success callbacks:

function DeleteBatchJS() {
    var shouldDelete = confirm('Delete the ENTIRE batch and all of its contents? ALL work will be lost.');
    if (!shouldDelete) {
        return;
    }

    var password = prompt('Please enter your password');
    var userInfo = get_cookie('UserInfo');
    PageMethods.AuthenticateUser(
        userInfo, 
        password,
        function(result) {
            // It's inside this callback that you have the result
            if (result) {
                PageMethods.DeleteBatchJSWM(
                    userInfo,
                    function(data) {
                        // It's inside this callback that you know if
                        // the batch was deleted or not
                        alert('Batch Deleted.');
                        window.location.href = 'BatchOperations.aspx';
                    }
                );
            }
        }    
    );
}
发布评论

评论列表(0)

  1. 暂无评论