【问题标题】:WebForm visual Studio 2013 two types, in which one type don't trigger WebMethodWebForm visual Studio 2013 两种,其中一种不触发WebMethod
【发布时间】:2015-08-17 09:33:37
【问题描述】:

我正在使用 Visual Studio 2013。 我尝试了这个示例代码,但无法理解我面临的一个问题。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>

<script type = "text/javascript">
function ShowCurrentTime() {
        $.ajax({
            type: "POST",
            url: "/Default.aspx/GetCurrentTime",
            data: '{name: "' + $("#<%=txtName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
Your Name :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
    onclick = "ShowCurrentTime()" />
</div>
    </form>
</body>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
    {

    }
    [System.Web.Services.WebMethod]
    public static string GetCurrentTime(string name)
    {
        return "Hello " + name  + "The Current Time is: "
            + DateTime.Now.ToString();
    }

现在我们在构建“Web 应用程序”时可以获得 2 种类型的 WebForms:-

  1. 在“选择模板”部分中选择“WebForm”。
  2. 或者在“模板”部分选择“空”,在“参考”部分勾选“WebForm”。

我知道这两者之间的基本区别,但为什么给定的代码不在 (1) 中运行,而是在 (2) 中运行。在 (1) 中,它给出“未定义”作为输出。

有人告诉我为什么会这样以及如何让它在 (1) 类型的 WebForm 中运行。

【问题讨论】:

标签: asp.net visual-studio-2013 webforms webmethod


【解决方案1】:

更改 $("#")[0].value 为 $("#").val() 并测试它。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多