2016-04-27 25 views
1

PageMethodsを使用してクライアントサイドからサーバー側の関数を呼び出し、JavaScriptの関数の戻り値にアクセスできますか?ここでは例です:クライアント側(JavaScript)のサーバー側(VB.NET)からの戻り値はどのように使用しますか?

クライアント側:

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 
} 

サーバーサイド:簡単に言えば

<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 

、私はちょうど上のサーバー側の関数の戻り値にアクセスできるようにする必要がありますクライアント側。前もって感謝します。

答えて

2

オンラインチュートリアルから盗まれたが、基本的には、この操作を行います。

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

これは働いていましたの!ありがとう! – ic3man7019

関連する問題