Simple ajax call doesn't work with a push of a button

I am using Encosia sample on my website on how to use ajax call

When I click on the div, it works fine, and when I replace the button instead of the div, it refreshes the whole page, and I find no errors in firebug.

Here is my code:

Script:

<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 content with the page method 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>

Code for:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function GetDate() As String
     Return DateTime.Now.ToString()
End Function
+3
source share
1 answer

What is most likely to happen - the button sends the page so that the pages reload. I used to have this problem in some browsers. You need to specify an attribute type="button".

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

+5
source