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出来
    }
}

 

posted @ 2016-04-24 22:18  预立科技  阅读(29)  评论(0)    收藏  举报