1. 程式人生 > >ASP.NET 使用Ajax 請求 aspx.cs 中的方法

ASP.NET 使用Ajax 請求 aspx.cs 中的方法

AJAX程式碼

$.ajax({
                    type: "post", //要用post方式                 
                    url: "../Item/HouseUpdate.aspx/Management",//方法所在頁面和方法名   是.aspx 檔案  而不是 .aspx.cs 
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        alert(data.d);//返回的資料用data.d獲取內容
                    },
                    error: function (err) {
                        alert(err);
                    }
                });

c# 程式碼

using System.Web.Services;

public partial class Item_HouseUpdate : System.Web.UI.Page
{
    [WebMethod]
    public static string Management()
    {
        JavaScriptSerializer serial = new JavaScriptSerializer();
        string value = "hello world!";
        return serial.Serialize(value);   //序列化返回json,前面可以alert出來
    }
}