简体   繁体   English

从 Javascript 调用 VB.NET WebMethod Function

[英]Calling VB.NET WebMethod Function from Javascript

I have a VB.NET function which looks like this:我有一个 VB.NET function 看起来像这样:

<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:我试图从 javascript 中调用它,如下所示:

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.它调用 function,但不会返回值。 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'.浏览代码时,我的 VB function 确实会触发(只要输入正确的密码,它就会返回 true),但 javascript 的“已验证”值仍然是“未定义”。 It's like you can't return values from VB functions to javascript.就像您无法将值从 VB 函数返回到 javascript。

I also tried我也试过

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

But still no luck.但仍然没有运气。

What am I doing wrong?我究竟做错了什么?

Thanks,谢谢,

Jason杰森

Web methods are invoked using AJAX, ie asynchronously, ie you have to wait until the method completes before consuming the results, ie you have to use the success callbacks: Web 方法使用 AJAX 调用,即异步调用,即您必须等到方法完成才能使用结果,即您必须使用成功回调:

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';
                    }
                );
            }
        }    
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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