从Javascript调用VB.NET WebMethod函数
我有一个VB.NET函数,如下所示:从Javascript调用VB.NET WebMethod函数,javascript,asp.net,vb.net,pagemethods,webmethod,Javascript,Asp.net,Vb.net,Pagemethods,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 fro
<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
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的“authenticated”值仍然是“undefined”。就像你不能从VB函数返回值到javascript一样
我也试过了
if PageMethods.AuthenticateUser("UserName", "Password")
{
//Stuff
}
但还是没有运气
我做错了什么
谢谢
Jason使用AJAX调用Web方法,即异步调用,即在使用结果之前必须等待方法完成,即必须使用成功回调:
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';
}
);
}
}
);
}
您还可以传入函数的地址和错误函数,如下所示well@Darin:我想你遗漏了
.d
,这可能是OPs真正的问题。@naveen,不,没有.d
。这是使用jQuery使用PageMethods的时候。ASP.NET自动生成的函数会自动删除它。@Darin:它的工作时间正好是1/2。通常在连续第4次或第5次尝试后,它会开始工作(当它再次开始不工作时,会有4次或5次)。@Json,这可能是因为您的服务器PageMethod在1/2的时间内抛出异常吗?你试过在里面放断点吗?您是否也尝试过用FireBug分析AJAX请求/响应?请注意,我永远不会提示输入这样的密码。@Joel--密码编码是为了简单起见。真正的代码更复杂一些