这是我第一次尝试使用 jQuery.ajax() 调用 WebMethod。我搜索并搜索了 stackoverflow,以及谷歌我不知道多少次,在这一点上,我觉得我只是在尝试我发现的随机修复,希望能有所作为。我当然已经停止学习了,所以我想是时候问一问了。
我的错误:未知的网络方法日期。参数名称:方法名称。
我的类(class)和 WebMethod:
[ScriptService]
public partial class _Maps : Page
{
protected void Page_PreLoad(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string Date()
{
return DateTime.Now.ToString();
}
}
我的页面:
<html>
<head>
<title>jQuery</title>
<script type ="text/javascript" src ="Scripts/jquery-1.8.2.min.js"></script>
<script type ="text/javascript" src ="Scripts/cSharp.js"></script>
</head>
<body>
<div id="clickArea" style="height: 1000px"></div>
</body>
</html>
和我的 Javascript:
$(document).ready(function ()
{
$("#clickArea").click(function ()
{
alert("Clicked");
$.ajax(
{
type: "POST",
url: 'Maps.aspx/Date',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg)
{
alert(msg);
$("#clickArea").text(msg.d);
},
error: function(xhr, msg, msg2)
{
alert(msg2);
alert(xhr.responseText);
}
});
});
});
我知道这个问题已被问过很多次,但我对所用技术的经验有限,而且我知道我一定漏掉了一些愚蠢的东西。我几乎整天都在做这个,我觉得我现在只是在兜圈子。如果需要,我可以发布更多信息。
谢谢。
最佳答案
终于解决了!
我需要做的就是在页面声明中设置我的 .ASPX 页面的 Inherits 属性。它找不到该方法,因为后面的代码不是我拥有 WebMethod 的实际页面。愚蠢的,就像我想的那样。
关于c# - 使用 jQuery.ajax() 访问 c# WebMethod 结果为 'Unknown Web Method',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18858661/