Jquery Ajax 复杂json对象提交到WebService

一.使用get方式

1.前台

 //复杂json对象提交
            var person = {'per':"{ 'id': 1, 'name': '张三', 'sex': '男' }"};
            $.ajax({
                type: "get",
                url: "JsonObject.asmx/GetPersonByObject",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    if (data.d == "1") {
                        $("#hello").text("服务器接收成功!");
                    }
                    else {
                        $("#hello").text("服务器接收数据失败!");
                    }
                },
                error: function () {
                    $("#hello").text("程序运行出错!");
                }
            });

2.后台:

 [WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json, UseHttpGet=true)]
        public string GetPersonByObject()
        {
            string jsonStr = HttpContext.Current.Request["per"];
            Person per = jsonStr.JsonDeserialezer<Person>();//将json字符串反序列化
            if (per.Id == 1)
            {
                return "1";
            }
            return "0";
        }

二.使用post方式

1.前台

var person = "{'per':\"{ 'id': 1, 'name': '张三', 'sex': '男' }\"}";
            $.ajax({
                type: "post",
                url: "JsonObject.asmx/GetPersonByObject",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    if (data.d == "1") {
                        $("#hello").text("服务器接收成功!");
                    }
                    else {
                        $("#hello").text("服务器接收数据失败!");
                    }
                },
                error: function () {
                    $("#hello").text("程序运行出错!");
                }
            });

2.后台

 [WebMethod]
        public string GetPersonByObject(string per)
        {
          Person person=   per.JsonDeserialezer<Person>();//将json反序列化
          if (person.Id == 1)
          {
              return "1";
          }
            return "0";
        }

三.List类型json提交,post方式

1.前台

 //复杂json对象提交2
            var person = "{'per':\"[{ 'id': 1, 'name': '张三', 'sex': '男' },{ 'id': 2, 'name': '王芳', 'sex': '女' }]\"}";
            $.ajax({
                type: "post",
                url: "JsonObject.asmx/GetPersonByOjects",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                        $("#hello").text("就收前台数据人数:"+data.d);
                },
                error: function () {
                    $("#hello").text("程序运行出错!");
                }
            });

2.后台

 [WebMethod]
        public int GetPersonByOjects(string per)
        {
            List<Person> list = per.JsonDeserialezer<List<Person>>();//反序列化json字符串
            return list.Count;
        }

四.List类型json提交,get方式

1.前台

var person = {'per':"[{ 'id': 1, 'name': '张三', 'sex': '男' },{ 'id': 2, 'name': '王芳', 'sex': '女' }]"};
            $.ajax({
                type: "get",
                url: "JsonObject.asmx/GetPersonByOjects",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                        $("#hello").text("就收前台数据人数:"+data.d);
                },
                error: function () {
                    $("#hello").text("程序运行出错!");
                }
            });

2.后台

 [WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json,UseHttpGet=true)]
        public int GetPersonByOjects()
        {
            string per = HttpContext.Current.Request["per"];
            List<Person> list = per.JsonDeserialezer<List<Person>>();
            return list.Count;
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值