第一種就是對應方法的請求 雖然對應方法 但還是會刷新頁面 webform是基於事件的 每次請求都會出發pageload
<script> $(function () { $("#btn").click(function () { $.ajax({ type: "POST", contentType: "application/json", url: "Index.aspx/SayHello", data: null, dataType: "json", success: function (msg) { alert(msg.d); } }); }); }); </script>
[System.Web.Services.WebMethod] public static string SayHello() { return "Hello"; }
第二種方法 就是直接請求 pageload
protected void Page_Load(object sender, EventArgs e) { _delWhere = ""; if (Request["where"] != null) { _where = Request["where"].ToString(); BindData(_where); }
}
$.ajax({ url: "BugListnew.aspx?where=" + where, type: "Post", dataType: "json", //請求到服務器返回的數據類型 data: { "where": where }, success: function(data) { var obj = $.parseJSON(data); //這個數據 var name = obj.Json[0].UserName; var age = obj.Json[0].Age; document.getElementById("name").innerHTML = name; document.getElementById("age").innerHTML = age; } })