Search code examples
javascriptasp.netasynchronouswebmethodpagemethods

Weird Asynchronous Javascript & WebMethod Behavior


I'm calling a PageMethod in javascript. Like this:

function DeleteBatchJS2()
  {$find('mdlPassword').hide();
    var pswd = $('#txtPassword').val();
    var userInfo = get_cookie("UserInfo");
    PageMethods.AuthenticateAndDelete(
        userInfo,
        pswd,
        onSuccess(),
        onError1());    
  }         

function onSuccess(result)
  {alert(result);}

function onError1(result)
  {alert(result);}

Now here's the strange part: One would think that calling PageMethods would give one (1) alert when running. Either the onSuccess function or the onError1 function. BUT -- I get two alerts, both saying "Undefined".

As a matter of fact, when I put a breakpoint in the VB code-behind (like the 3rd or 4th line of code in the function), I get BOTH alert boxes before I can step into my code behind. Two alerts, and THEN my code breaks.

This makes no sense to me. Am I missing anything?

Thanks,

Jason.

P.S. -- Here's the source for the WebMethod function. Please also note it does make a WCF call.

<WebMethod()> _
Public Shared Function AuthenticateAndDelete(ByVal UserInfo As String, ByVal Password As String) As Boolean
    Dim Client As New LetterWriterClient
    Dim bo As New BatchOperations
    Dim UserNumber As String
    Dim UserName As String


    'Extract the user name and number from the user info cookie string
    UserName = GetValueFromVBCookie("UserName", UserInfo)
    UserNumber = GetValueFromVBCookie("UserNumber", UserInfo)

    'Now validate the user
    If bo.ValidateActiveDirectoryLogin("Backoffice", UserName, Password) Then
        AuthenticateAndDelete = Client.Delete_dat_BatchSQL(UserNumber)
        Client.Close()
    Else
        AuthenticateAndDelete = False
    End If

End Function

Solution

  • Should be:

    PageMethods.AuthenticateAndDelete(
            userInfo,
            pswd,
            onSuccess,
            onError1);    
      }