在ajax的已不请求中,常常返回json对象。可以利用json.net给我们提供的api达到快速开发。

例子:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Newtonsoft.Json;//这是json.net的命名空间

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
    {
        StudentInfo info = new StudentInfo();
        info.Sname = "dld";
        info.Gender = "女";
        info.Age = 33;
        string str = JsonConvert.SerializeObject(info);
        Response.Write(str);
    }
}
public class StudentInfo
{
    public string Sname { get; set; }
    public string Gender { get; set; }
    public byte Age { get; set; }
}

 
 
 
 
 
 
 
 
 
。使用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的更多相关文章

  1. .Net之使用Jquery Ajax通过FormData对象异步提交图片文件到服务端保存并返回保存的图片路径

    前言: 首先对于图片上传而言,在我们的项目开发中可以说出现的频率是相当的高的.这篇文章中,我将要描述的是在我们.Net中如何使用Jquery Ajax通过FormData对象异步提交图片文件到后台保存 ...

  2. Ajax--PHP+JQuery+Ajax解析json、XML数据、加载页面

    一.JQuery+Ajax用get.post方式提交和请求数据 知识要点: $('#userName').blur(function () { var txt = $(this).val(); $.a ...

  3. 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据

    jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...

  4. jquery ajax返回json数据进行前后台交互实例

    jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...

  5. Spring MVC Controller与jquery ajax请求处理json

    在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: "}]}]} $.ajax({ url : url, typ ...

  6. ajax获取json对象

    ajax获取json对象 ajax获取json数据,都是一个原理,设置response 的Content-Type:application/json,这样浏览器自动会解析为json对象 $result ...

  7. JQuery ajax返回JSON时的处理方式

    最近在使用JQuery的ajax方法时,要求返回的数据为json数据,在处理的过程中遇到下面的几个问题,那就是采用不同的方式来生成json数据的时候,在$.ajax方法中应该是如何来处理的,下面依次来 ...

  8. jQuery ajax 传递JSON数组到Spring Controller

    jQuery ajax传递单个JSON对象到后台很容易,这里记录的是传递多个JSON对象组成的JSON数组到java 后台,并说明java如何解析JSON数组. 1.js代码 var relation ...

  9. ajax返回json对象的两种写法

    1. 前言 dataType: 要求为String类型的参数,预期服务器返回的数据类型.如果不指定,JQuery将自动根据http包mime信息返回responseXML或responseText,并 ...

随机推荐

  1. Could not find any resources for the specified culture or the neutral culture

    问题解决办法是: 打开资源文件,将access modifier:下拉项设置为“internal”即可

  2. @Configurable

    spring的一个注解,用来自动注入bean的注解,不需要通过BeanFactory去获取    

  3. mysql主从报错截图

    1.首先确认报错的binlog和last event. 2.然后去主的机器上查看该报错的内容和报错的的位置,如果是结尾报错的,可以直接从新的binlog进行同步. #/mysql/logs# ../b ...

  4. “全栈2019”Java多线程第二章:创建多线程之继承Thread类

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. eclipse的debug模式经常自动跳到TheadPoolExecutor的断点

    1.问题:eclipse的debug模式经常自动跳到TheadPoolExecutor的断点,实在烦人 解决方法是: 在eclipse中选择Window->Preference->Java ...

  6. Python Pymongo中Connection()与MongoClient()差异

    在git找了几个blog的源码,在学习的过程中,发现有人使用Connection(),有人却在使用MongoClient(),那么到底两者有什么差别呢? 且看分析如下: db = Connection ...

  7. [Objective-C语言教程]多态(26)

    多态性这个词表示有许多形式. 通常,当存在类的层次结构并且通过继承相关时,会发生多态性. Objective-C多态表示对成员函数的调用将导致执行不同的函数,具体取决于调用该函数的对象的类型. 考虑下 ...

  8. Junit 测试exception

    有两种方法: 一.使用ExpectedException 仅在junit4.7以上支持.不仅可以测试捕获到某异常,也可以测试异常message. 使用例子如下: @Rule public Expect ...

  9. webpack打包报错Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead

    npm install –save-dev extract-text-webpack-plugin@next 会下载到+ extract-text-webpack-plugin@4.0.0-beta. ...

  10. [转] gitlab 的 CI/CD 配置管理

    [From] http://blog.51cto.com/flyfish225/2156602 gitlab 的 CI/CD 配置管理 (二) 标签(空格分隔):运维系列 一:gitlab CI/CD ...