gpt4 book ai didi

c# - PageMethods 返回未定义的结果?

转载 作者:太空狗 更新时间:2023-10-29 20:09:09 27 4
gpt4 key购买 nike

我有一个非常简单的 PageMethod 调用。当我单步执行 .cs 文件中的 PageMethod 时,该值看起来符合预期。但是,在客户端,我得到一个未定义的结果。有任何想法吗?这应该非常简单。

这是我的 js:(EnablePageMethods="true" 在我的 ASPX 页面中)

function test() {
alert(PageMethods.MyMethod("Joe Blow"));
}

这是我的 C#:

public partial class test : System.Web.UI.Page 
{
[WebMethod]
public static string MyMethod(string name)
{
return "Hello " + name;
}
}

最佳答案

这是关于如何使用 MS Ajax 调用 PageMethods 的答案。首先确保您已经从 MS 网站下载了最新的 Ajax 库。

<asp:ScriptManager ID="sm1" runat="server" EnablePageMethods="true">   
</asp:ScriptManager>

<input type="button" value="Greeting" onclick="greetings()" />

<script language="javascript" type="text/javascript">

function greetings() {

PageMethods.GreetingFromPage(function(response) {

alert(response);

});

}


</script>

[WebMethod]
public static string GreetingFromPage()
{
return "greeting from page";
}

差不多就这些了!

关于c# - PageMethods 返回未定义的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1324178/

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