將普通頁面的方法公佈為WebMethod,以Javascript形式訪問。下面請看yjbys小編對於JS AJAX前台如何給後台類的函數傳遞參數的方法介紹。
1 方法要public static修飾,返回類型最好是string。
2 方法前添加[WebMethod] 特性。
3 Client端訪問時要使用Post方法,和Json作為數據形式進行交互。否則會整頁HTML返回。
4 在jQuery訪問時,回調中的data.d才時真正的返回內容。
例:
[WebMethod]
public static string GetTime()
{
return ring("yyyy-MM-dd HH:mm:ss");
}
---------------
腳本(以jQuery為例調用)
$({
url:url,
method:"post",
dataType:"json",
contentType:"application/json; charset=UTF-8",
success: function(data){
$("#id")(data.d); //見第3點
}
});