首先,這么用是不好的。最好用ashx,但也難免遇到這種需求。開發過這么一個系統,每天訪問量最多100,web服務器壓力很小,完全大馬拉小車,主要壓力都在數據庫服務器上,要做大量的統計。所以頁面直接全上服務器控件搞定。用到ajax的時候也懶得再寫個ashx了,直接aspx里寫了。下面是例子:
前端:
1 function AjaxRquest() { 2 $.ajax({ 3 url: location.href, 4 type: "POST", 5 data: { "RequestType": "AjaxRequest", "id": "1" },//模擬個數據 6 success: function(data) { 7 alert(data); 8 } 9 }); 10 }
后台:
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (Request["RequestType"] == "AjaxRequest") 4 { 5 string id = Request["id"]; 6 Response.Clear(); 7 Response.Write("ID : " + id + " ," + DateTime.Now); 8 Response.End(); 9 return; 10 } 11 }