gpt4 book ai didi

c# - ASP.NET 可视化 Web 部件 + AJAX,无法使其正常工作

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

我试图实现这个简单的例子 http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx

但是不行,它给了

Failed to load http://site/path/path/pagename.aspx/GetCurrentTime resource: the server responded with a status of 500 (Internal Server Error)

客户端

<script src="http://.../jquery-1.7.2.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
$.ajax({
type: "POST",
url: "pagename.aspx/GetCurrentTime",
data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
onclick = "ShowCurrentTime()" />

服务器(.cs 文件)

        [System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}

最佳答案

ajax 定义中没有这样的函数failure。您需要将其重命名为 error。检查来源:jQuery.ajax()

P.S 我还建议您使用 console.log,而不是警报。 console.log 您的名称参数以查看是否返回正确的值。检查JSON.stringify用于创建您的数据。

关于c# - ASP.NET 可视化 Web 部件 + AJAX,无法使其正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40673316/

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