Webform 做方法讓 ajax呼叫
首先 因為在Webform不像Asp.Net MVC一樣可以直接做成Api呼叫url就可以直接呼叫到後端的Method
所以我查了一下要怎麼使用 Webform做到相同功能,必須要在上面加上[WebMethod]這個 Attribute
下面是我示範的方法
C# Post呼叫
       [WebMethod]
        public static string GetData(testmodel postData)
        {
            string user = postData.User;
            string Phone = postData.Phone;
            return "test";
        }
ajax的呼叫方法
     var postData =  {User: 'JOE',Phone:'0900222333'};
            alert(id);
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetData",     
                data: JSON.stringify({ 'postData': postData}) ,
                contentType: "application/json; charset=utf-8",
                dataType: "json",     
                success: function (data) { alert('success'); }      
            });
如果是用Get的話 (上面記得要用 [ScriptMethod(UseHttpGet = true)]這個 Attribute )
      [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public static string GetData(string User)
        {
            return User;
        }
ajax的部分
            $.ajax({
                type: "GET",
                url: "Default.aspx/GetData?user=123",
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { alert(data.d); }
            });
以上就是完整的ajax 呼叫Webform的方法