1. 程式人生 > >ajax請求aspx.cs後臺方法

ajax請求aspx.cs後臺方法

前臺jquery程式碼

 $(function () {
      $("#btnfix").click(function () {
                 $.ajax({
                         type: "post",
                         url: "fix.aspx/test",
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",
                         success: 
function (data) { console.log(data.d); }, error: function (err) { alert(err); } }); }); });

後臺程式碼

using System.Web.Services; //引用Web.Services名稱空間
public partial class Fix : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] //新增Attribute public static string test() //方法設為靜態 { return "hello world"; } }

執行結果