How do you use a return value from the server side (VB.NET) on the client side (JavaScript)?

旧时模样 提交于 2019-12-12 17:20:41

问题


Is it possible to call a function on the server side from the client side using PageMethods, and access that function's return value in JavaScript? Here's an example:

Client Side:

function onBlur(){ //this function is called when you click away from a textbox
    PageMethods.SomeServerSideFunction(params, onSuccess, onFailure);
}

function onSuccess(){
    //do something on success
    //I want to be able to print out the server side function's return value here
     alert(PageMethods.DoesItWork(params)) //this is what I've tried, but it doesn't work 
}

function onFailure(){
    //do something on failure
}

Server Side:

<System.Web.Services.WebMethod()>
Public Shared Function DoesItWork(params As String) As Boolean
    'logic to determine a return value
     Dim RetVal as Boolean
     Return RetVal 'I want to be able to use this return value on the client side
End Function

Put simply, I just need to be able to access the server side function's return value on the client side. Thanks in advance.


回答1:


Stolen from a tutorial online, but basically you do this:

[System.Web.Services.WebMethod]
public static string ToUpper(string data) {
  return data.ToUpper();
}

--

function CallMethod() {
  PageMethods.ToUpper("hello", OnSuccessCallback, OnFailureCallback);
}

function OnSuccessCallback(res) {
  alert(res);
}

function OnFailureCallback() {
  alert('Error');
} 


来源:https://stackoverflow.com/questions/36894814/how-do-you-use-a-return-value-from-the-server-side-vb-net-on-the-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!