• 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;
            }
  • 相关阅读:
    模拟赛20181101 雅礼 Wearry 施工 蔬菜 联盟
    模拟赛20181031 雅礼 Wearry 养花 折射 画作
    set/priority_queue的运算符重载
    set的完整用法
    最长公共上升子序列 O(n^2)
    无向图边双联通分量 tarjan 模板
    ID 迭代加深搜索 模板 埃及分数
    树上背包DP Luogu P2014 选课
    A* 第k短路
    [POJ3468]关于整数的简单题 (你想要的)树状数组区间修改区间查询
  • 原文地址:https://www.cnblogs.com/hclw/p/4413061.html
Copyright © 2020-2023  润新知