gpt4 book ai didi

c# - ajax调用C#方法

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:04 24 4
gpt4 key购买 nike

我想使用 jquery ajax 调用 C# 方法,该方法不返回任何东西,而是直接修改 aspx 页面中的数据。我的调用工作正常,但我无法通过该方法对 UI 进行任何更改。

jQuery

    $(document).ready(function () {

$('#<%=Button1.ClientID %>').click(function () {
debugger;
$.ajax({
type: "GET",
url: "WebForm1.aspx/ServerSideMethod",
data: "{}",
<%--contentType: "application/json; charset=utf-8",
dataType: "json",--%>
async: true,
cache: false,
success: function (msg) {
alert("E");

$('#myDiv').text(msg.d);
},
error: function (err) {
alert("Error");
},
failure: function (response) {
alert("ror1");
}
})
return false;
});
});
</script>

C#

[WebMethod]

public string ServerSideMethod(){
Label1.Text="Hi";
return "Hi ajax call to C# method";
}

注意:必须仅在函数中实现的更改,如果我删除了 javascript 中的注释部分,那么 ajax 调用将向我抛出错误警报消息

最佳答案

ServerSideMethod 应声明为static:

[WebMethod]
public static string ServerSideMethod()

并且您无法从 ServerSideMethod 内部更改 Label1.Text,因为它是 static

关于c# - ajax调用C#方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21382232/

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