JSONP

01.先定义一个Person类

 /// 
    /// 包含用户的基本信息
    /// 
    public class Person
    {
        /// 
        /// 获取或设置用户名
        /// 
        public string Name { get; set; }

        /// 
        /// 获取或设置用户年龄
        /// 
        public int Age { get; set; }

        /// 
        /// 获取或设置用户性别
        /// 
        public string Gender { get; set; }

        /// 
        /// 获取或设置用户国籍
        /// 
        public string Country { get; set; }

        /// 
        /// 获取或设置用户电子邮箱
        /// 
        public string Email { get; set; }
    }

02.为Person集合添加数据

  public static List GetPersons()
        {
            List ps = new List();
            Person p1 = new Person { Name = "Tom", Age = 32, Country = "US", Gender = "Male", Email = "[email protected]" };
            Person p2 = new Person { Name = "Jack", Age = 23, Country = "UK", Gender = "Male", Email = "[email protected]" };
            Person p3 = new Person { Name = "Eden", Age = 25, Country = "Canada", Gender = "Female", Email = "[email protected]" };
            Person p4 = new Person { Name = "Li Hua", Age = 29, Country = "China", Gender = "Male", Email = "[email protected]" };
            Person p5 = new Person { Name = "Lvo", Age = 40, Country = "US", Gender = "Male", Email = "[email protected]" };
            ps.Add(p1);
            ps.Add(p2);
            ps.Add(p3);
            ps.Add(p4);
            ps.Add(p5);
            return ps;
        }

03.服务端代码

 

 public void ProcessRequest(HttpContext context)
        {
            string callbackFun = context.Request["CallBackFun"]; //客户端执行的方法名称
            List person = PersonRepository.GetPersons();
            string json = JsonConvert.SerializeObject(person); //Json.Net序列化
            string fun = callbackFun + "(" + json + ")";
            context.Response.Write(fun);

        }

 

04.客户端代码

 




    
    
    
    
    


    
    

 

转载于:https://www.cnblogs.com/sumg/p/3904403.html

你可能感兴趣的:(JSONP)