2012-05-12 17 views
3

をクリックして、私はそれがうまく働いていると私は代わりにdiv要素のボタンを交換するとき、それは全体をリフレッシュだのdivをクリックするとAJAX呼び出しシンプルなAJAX呼び出しは

の使用方法についての彼のウェブサイトからEncosia'sサンプルを使用しています私は火かき棒で何の誤りも見つけられません。ここで

は私のコードです:

スクリプト:

<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 

答えて

5

ほとんどの場合、ボタンがページを送信してページがリロードされます。私はいくつかのブラウザでこれまでにこの問題を抱えていました。 type="button"属性を指定する必要があります。 correctly.Itは今働いていたことを認識するためのアンドレイ・感謝@

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

+0

。 – coder

+0

私はそれが助けてうれしいよ:) – Andrey