Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  147] [ 7]  / answers: 1 / hits: 23542  / 13 Years ago, tue, july 12, 2011, 12:00:00

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


More From » asp.net

 Answers
50

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

[#91220] Monday, July 11, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brendan

Total Points: 426
Total Questions: 110
Total Answers: 94

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;