.net中Ajax常见使用

一 .  使用Ajsx获取.aspx.cs后台数据

 1. 前台 $.ajax({
              type: "POST",
              contentType: "application/json",
              url: "c.aspx/GetAjaxData", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
              data: "{'name':'bill','age':'28','height':'175'}",  //这里是要传递的参数,注意参数名跟后台方法参数名的对应
              dataType: 'json',   //WebService 返回Json类型 或者 Json/string  
              success: function (re) {
              var a = eval("(" + re.d + ")");//后台返回是字符串,所以转换为json对象
               alert(a.Name+a.Age+a.Height);
          }

2. 后台(别忘了引用 using System.Web.Services;)

    [WebMethod]
    public static string GetAjaxData(string name,string age,string height)
    {
        Person p = new Person();
        p.Name = name;
        p.Age = age;
        p.Height = height;
        JavaScriptSerializer json = new JavaScriptSerializer();
        string pJson = json.Serialize(p);
        return pJson;
    }

二 返回的是字符串 还是对象

1前台

      $.ajax({
                //                type: "POST",
                //                contentType: "application/json",
                //                url: "c.aspx/GetAjaxData", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
                //                data: "{'name':'bill','age':'28','height':'175'}",  //这里是要传递的参数,注意参数名跟后台方法参数名的对应
                //                dataType: 'json',   //WebService 返回Json类型 或者 Json/string  
                //                success: function (re) {
                //                    var a = eval("(" + re.d + ")");
                //                    alert(a.Name+a.Age+a.Height);
                //                }


                //                dataType: 'json/string',   //WebService 返回Json类型 或者 Json/string  
                //                success: function (re) {
                //                    alert(re);
                //                    var a = eval("(" + re + ")"); //转换为json对象
                //                    var bb = eval("(" + a.d + ")");
                //                    alert(bb.Name+bb.Age+bb.Height);
                //                }


                type: "POST",
                contentType: "application/json",
                url: "c.aspx/GetAjaxDataObject", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
                data: "{'name':'bill','age':'28','height':'175'}",  //这里是要传递的参数,注意参数名跟后台方法参数名的对应
//                dataType: 'json/string',   //WebService 返回Json类型 或者 Json/string  
//                success: function (re) {
//                    //var a = re;
//                    //alert(re);
//                    var a = eval("(" + re+ ")");
//                    //alert(a.d);
//                    alert(a.d.Name + a.d.Age + a.d.Height);
//                }

                dataType: 'json',   //WebService 返回Json类型 或者 Json/string  
                success: function (re) {
                    var a = re;
                    //alert(re);
                    //var a = eval("(" + re+ ")");
                    //alert(a.d);
                    alert(a.d.Name + a.d.Age + a.d.Height);
                }


            });

 

2  后台

[WebMethod]
    public static string GetAjaxData(string name,string age,string height)
    {
        Person p = new Person();
        p.Name = name;
        p.Age = age;
        p.Height = height;
        JavaScriptSerializer json = new JavaScriptSerializer();
        string pJson = json.Serialize(p);
        return pJson;
    }
    [WebMethod]
    public static Person GetAjaxDataObject(string name, string age, string height)
    {
        Person p = new Person();
        p.Name = name;
        p.Age = age;
        p.Height = height;
        return p;
    }



public class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private string age;

    public string Age
    {
        get { return age; }
        set { age = value; }
    }
    private string height;

    public string Height
    {
        get { return height; }
        set { height = value; }
    }
}

 

 

 

 

 

 

 

 

 

  

 

posted @ 2013-07-27 14:12  fulai_xy  阅读(228)  评论(0编辑  收藏  举报