gpt4 book ai didi

javascript - ASP.NET JavaScript 函数未定义

转载 作者:行者123 更新时间:2023-12-01 05:27:42 25 4
gpt4 key购买 nike

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

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/

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