將普通頁面的方法公布為WebMethod,以Javascript形式訪問。
1 方法要public static修飾,返回類型最好是string。
2 方法前添加[WebMethod] 特性。
3 Client端訪問時要使用Post方法,和Json作為數據形式進行交互。否則會整頁HTML返回。
4 在jQuery訪問時,回調中的data.d才時真正的返回內容。
5 訪問URL為: 如有個GetTime的公共靜態方法。
例:
abc.aspx
[WebMethod]public static string GetTime(){return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");}
---------------
腳本(以jQuery為例調用)
$.ajax({url:url,method:"post",dataType:"json",contentType:"application/json; charset=UTF-8",success: function(data){$("#id").html(data.d); //見第3點}});
新聞熱點
疑難解答
圖片精選