如何在客户端(JavaScript)上使用服务器端(VB.NET)的返回值? [英] How do you use a return value from the server side (VB.NET) on the client side (JavaScript)?

查看:52
本文介绍了如何在客户端(JavaScript)上使用服务器端(VB.NET)的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用PageMethods从客户端在服务器端调用一个函数,并在JavaScript中访问该函数的返回值?这是一个示例:

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:

客户端:

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

简单地说,我只需要能够在客户端访问服务器端函数的返回值.提前致谢.

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

推荐答案

从在线教程中被盗,但基本上您可以这样做:

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

这篇关于如何在客户端(JavaScript)上使用服务器端(VB.NET)的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆