分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

Jquery Ajax 复杂json对象提交到WebService

发布时间:2023-09-06 01:27责任编辑:沈小雨关键词:jsjsonWebAjax
。使用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;
        }
 
 
 
http://blog.163.com/m13864039250_1/blog/static/213865248201373105642827/
 

Jquery Ajax 复杂json对象提交到WebService

原文地址:http://www.cnblogs.com/yyzyou/p/7899218.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved