• .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; }
        }
    }

      

  • 相关阅读:
    Scrapy框架
    描述符类
    完整的blog设计过程
    钉钉机器人设置步骤
    homework登录和支付mock两个接口
    jsonpath的用法和nnlog的使用
    mp4格式文件转码后处理(qt-faststart).md
    html5视音频标签参考.md
    ffmpeg文档43-开发者
    ffmpeg文档42-参考/看
  • 原文地址:https://www.cnblogs.com/fulai/p/3219466.html
Copyright © 2020-2023  润新知