从 Javascript 调用 VB.NET WebMethod 函数
我有一个 VB.NET 函数,如下所示:
<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
我尝试从 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.");}}}
它调用该函数,但不会返回值。当浏览代码时,我的 VB 函数确实会触发(只要输入正确的密码,它就会返回 true),但 javascript“已验证”值仍然是“未定义”。就像您无法将 VB 函数的值返回到 javascript 一样。
我也尝试过
if PageMethods.AuthenticateUser("UserName", "Password")
{
//Stuff
}
但仍然没有运气。
我做错了什么?
谢谢,
杰森
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Web 方法是使用 AJAX 调用的,即异步调用,即您必须等到方法完成才能使用结果,即您必须使用成功回调:
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: