简体   繁体   English

使用jQuery.ajax()访问c#WebMethod会导致“未知Web方法”

[英]Using jQuery.ajax() to access c# WebMethod results in 'Unknown Web Method'

This is my first attempt at using jQuery.ajax() to call a WebMethod. 这是我第一次尝试使用jQuery.ajax()调用WebMethod。 I have searched and searched stackoverflow, as well as Google I don't know how many times, and at this point I feel I'm just trying random fixes I find, hoping something works. 我已经搜索并搜索了stackoverflow,还有Google,我不知道有多少次,这时我觉得我只是在尝试找到的随机修复程序,希望能奏效。 I've certainly stopped learning, so I thought it time to ask. 我当然已经停止学习,所以我认为是时候问了。

My error: Unknown web method Date. 我的错误:未知的网络方法日期。 Parameter name: methodName. 参数名称:methodName。

My class and WebMethod: 我的课程和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();
    }
}

my page: 我的页面:

<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>

and my Javascript: 和我的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);
                }
            });
    });
});

I know this question has been asked numerous times, but I have limited experience with the technologies I'm using, and I know I must be missing something silly. 我知道这个问题已被问过无数次,但是我对所用技术的经验有限,而且我知道我一定会错过一些愚蠢的东西。 I've been at this almost all day, and I feel I am just running in circles now. 我几乎整天都呆在这里,感觉到我现在只是圈子里跑步。 I can post more information if needed. 如果需要,我可以发布更多信息。

Thanks. 谢谢。

Solved it, finally! 解决了,终于!

All I needed to do was set the Inherits property of my .ASPX page in the page declaration. 我要做的就是在页面声明中设置.ASPX页面的Inherits属性。 It could not find the method, as the code behind was not my actual page that has my WebMethod. 它找不到该方法,因为后面的代码不是我的实际页面,其中包含我的WebMethod。 Silly, like I had thought. 像我想的那样傻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM