JS AJAX前臺如何給后臺類的函數(shù)傳遞參數(shù)

字號:


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