gpt4 book ai didi

javascript - 如何在客户端 (JavaScript) 使用服务器端 (VB.NET) 的返回值?

转载 作者:行者123 更新时间:2023-11-29 19:13:41 27 4
gpt4 key购买 nike

是否可以使用 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');
}

关于javascript - 如何在客户端 (JavaScript) 使用服务器端 (VB.NET) 的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36894814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com