【问题标题】:Simple ajax call not working with button click简单的 ajax 调用不适用于按钮单击
【发布时间】:2012-05-21 00:42:38
【问题描述】:

我正在使用他网站上关于如何使用 ajax 调用的Encosia's 示例

当我点击 div 时,它工作正常,当我替换按钮而不是 div 时,它会刷新整个页面,我在 firebug 中没有发现任何错误。

这是我的代码:

脚本:

<script type="text/javascript">
    $(document).ready(function () {
        // Add the page method call as an onclick handler for the div.
        $("#getdate").click(function () {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetDate",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").html(msg.d);
                }
            });
        });
    });
</script>

HTML:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
<div id="Result">Click here for the time.</div>
<button id="getdate" value="Click Me">ClickMe</button>

代码隐藏:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function GetDate() As String
     Return DateTime.Now.ToString()
End Function

【问题讨论】:

    标签: asp.net ajax button click


    【解决方案1】:

    最有可能发生的是 - 按钮提交页面,因此页面被重新加载。我之前在某些浏览器中遇到过这个问题。 您需要指定type="button" 属性。

    http://www.w3schools.com/tags/tag_button.asp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2019-12-28
      • 2015-01-25
      相关资源
      最近更新 更多