我正在使用他网站上关于如何使用 ajax 调用的 Encosia示例
当我单击 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