【问题标题】:Uncaught Reference Error: function is not defined (ASP.NET)未捕获的引用错误:未定义函数 (ASP.NET)
【发布时间】:2017-08-17 01:22:11
【问题描述】:

我正在自学如何使用 ASP.NET,并尝试在我的 .aspx 文件中的 JS 函数中调用 C# 函数。

这是我的 JS 部分:

<script type="text/javascript">
        function Run() {
        alert("Running!")
        var ranking = parseInt(document.getElementById("rank").value);
        var collsize = parseInt(document.getElementById("size").value);
        var collsafety = 
           parseInt(document.getElementById("safety").value);
        var best = PageMethods.Survey(ranking, collsize, collsafety);
        document.write(best);
    }
</script>

C#函数的签名(像JS函数一样嵌入在.aspx文件中):

<script language="c#">
    [System.Web.Services.WebMethod]
    public static string Survey(int ranking, int collsize, int 
       collsafety) {
       ....
    }

确切的错误消息是:Uncaught ReferenceError: PageMethods.Survey is not defined。

我正在使用 onclick 调用带有按钮的 Run 方法。我知道输入了该方法,因为第一个警报发生了。但是,当我尝试调用调查时,我得到了错误。

我已经阅读了几个答案,例如:jquery PageMethod saying the method does not exist,但建议的 AJAX 也不起作用。我也读过这个:https://www.codeproject.com/Questions/561226/errorpluspageMethodplusisplusundefined 但我看不出我做错了什么......

【问题讨论】:

    标签: javascript c# asp.net


    【解决方案1】:

    您的代码将无法工作,因为您将 c# 代码直接嵌入到 aspx 文件中。将其嵌入到 aspx.cs 文件中:

    protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
    [System.Web.Services.WebMethod]
    public static string Survey(int ranking, int collsize, int 
       collsafety) 
      {
         return "Hello";
      }
    

    您可以通过 PageMethods 调用它:

        <script type="text/javascript">
    
            function Run() {
            var ranking = 123;
            var collsize = 123;
            PageMethods.Survey(ranking, collsize, onSucess, onError);
    
            function onSucess(result) {
                alert(result);
            }
    
            function onError(result) {
                alert('Please contact administrator.');
            }
        }
    </script>
    

    最后不要忘记在调用页面上添加 ScriptManager(在我的示例中为 Default.aspx)并将 PageMethods 启用为 True:

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
    </asp:ScriptManager>
    

    这里的外观供进一步参考:

    我在按钮上调用脚本时的代码:

     <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Run()" />
    

    【讨论】:

    • 我实际上切换到了 AJAX,现在它没有抛出那个特定的错误(我第一次没有正确地完成 AJAX)。但现在我收到 POST 404 Not Found 错误:(
    • 你可以用 AJAX 做到这一点,但我简化了我的解释,因为你问的是如何在 js 函数上调用 c#。
    • 谢谢。您是否知道可能导致 AJAX 错误的原因? (下一条评论中的代码 - 对格式感到抱歉)
    • function Run() { $.ajax({ type: 'POST', url: '@Url.Action("Survey", "Survey.aspx")', data: { 排名: 文档.getElementById("rank").value, collsize: document.getElementById("size").value, collsafety: document.getElementById("safety").value }, contentType: 'application/json; charset=utf-8' , dataType: 'json', 成功: function (msg) { alert(msg.d); $('#resultDisplay').replaceWith(msg.d); } }); }
    • 试试这个:url: 'Survey.aspx/Survey'
    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2011-09-25
    • 2021-11-01
    • 2017-11-22
    相关资源
    最近更新 更多