是否可以使用从客户端调用服务器端的函数PageMethods
,并在JavaScript中访问该函数的返回值?这是一个例子:
客户端:
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
简而言之,我只需要能够在客户端访问服务器端函数的返回值。提前致谢。
从在线教程中被盗,但基本上您可以这样做:
[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');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句