2

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 completed = 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

2
  • side note, I would never prompt for a password like that. 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 Commented Jul 12, 2011 at 18:54

1 Answer 1

4

Web methods are invoked using AJAX, i.e. asynchronously, i.e. you have to wait until the method completes 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';
                    }
                );
            }
        }    
    );
}
Sign up to request clarification or add additional context in comments.

6 Comments

You can also pass in the address of a function, and a error function as well
@Darin: i think you are missing out .d which might be OPs real problem.
@naveen, no, there is no .d. This is when you consume PageMethods with jQuery. The ASP.NET autogenerated functions remove it automatically.
@Darin: It works exactly 1/2 of the time. Usually after the 4th or 5th consecutive try, it starts working (for 4 or 5 times, when it starts not working again).
@Json, could that be because your server PageMethod is throwing an exception 1/2 of the time? Did you try putting breakpoints in it? Also did you try analyzing the AJAX requests/responses with FireBug?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.