javascript - ASP.NET JavaScript 函数未定义

标签 javascript jquery asp.net

我已经尝试调试以下错误几个小时,但没有解决方案(我想将键盘分成两半)。请帮忙。我需要它。

Unhandled exception at line 90, column 95 in http://localhost:51770/SimpleAjax.aspx

0x800a1391 - JavaScript runtime error: 'callAjaxMethod' is undefined

从页面上的按钮调用此方法:

<asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClientClick="callAjaxMethod()"/>

JS:

function callAjaxMethod()
{

   // e.preventDefault();

    $.ajax({
        type: "POST",
        url: "SimpleAjax.asmx/IsLeapYear",
        data: '{year: "' + $("#<%=txtYear.ClientID%>").val() + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            if (response.d) {
                $('#<%=txtResult.ClientID%>').val('Leap Year');
            }
            else {
                $('#<%=txtResult.ClientID%>').val('Not a Leap Year');
            }
        },
        failure: function (response) {
            $('#<%=txtResult.ClientID%>').val("Error in calling Ajax:" + response.d);
        }
    });
}

ASMX 文件中的代码:

    /// Summary description for SimpleAjax
    /// </summary>
    [WebService(Namespace = "http://automatedpumpcontroller.somee.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class SimpleAjax : System.Web.Services.WebService
    {

        [WebMethod]
        [System.Web.Script.Services.ScriptMethod()]

        public static bool IsLeapYear(int year)
        {
            return DateTime.IsLeapYear(year);
        }
    }
}

最佳答案

如果您的函数 callAjaxMethod 位于单独的 js 文件中,那么您需要将该文件包含在您的页面上,如下所示

例如带有名称的 Js 文件保存在 js\app.js 中,您的页面是 SimpleAjax.aspx

然后在 SimpleAjax.aspx 上 您需要在 head 中添加 script 标签

<script src="js\app.js"></script>

关于javascript - ASP.NET JavaScript 函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009432/

相关文章:

c# - 将字符串 append 到标签

javascript - 从 Chrome 应用中的剪贴板获取当前文本

javascript 日期返回未定义

jquery - 选择下拉菜单的 - 目标默认标题

JQuery 对话框和背景卡住

asp.net - 如何找出在我的 Windows 域上托管 LDAP 的服务器?

javascript - KnockoutJS - 如何使用 Observable 数组在 foreach 中隐藏某些元素?

javascript - 加载后清除 AJAX 结果

javascript - 在 Javascript 上用大写字母替换比较

javascript - onClick 函数在另一个函数中