在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. Spring框架注解

    这四个注解,功能都是一样的,都是用来创建对象的. 但是为什么有这么四个吗?Spring中提供了三个@Component的衍生注解:(功能目前来讲是一样的) @Controller      :WEB层 ...

  2. Java中将图片保存到数据库中

    在实际的开发中,我们可能需要将图片.影音等文件直接保存到数据库中,然后通过编程方式将数据读出进行使用.例如将读出的图片数据显示出来,将读出的电影文件播放出来. 二进制数据直接保存到文件和从文件中读出非 ...

  3. php pdo prepare真的安全吗

    详见 这里 Let's say I have code like this: $dbh = new PDO("blahblah"); $stmt = $dbh->prepar ...

  4. leetcode-686-Repeated String Match(重复多少次A能够找到B)

    题目描述: Given two strings A and B, find the minimum number of times A has to be repeated such that B i ...

  5. vue封装插件并发布到npm上

    vue封装插件并发布到npm上 项目初始化 首先,要创建项目,封装vue的插件用webpack-simple很合适,vue init webpack-simple 项目名称此命令创建我们的项目的目录, ...

  6. node-redis模块需要注意的事项

    node之中连接redis使用的redis模块,虽然好用,但是有些地方还是需要注意. npm install redis redis client 行为:1.客户端执行过程中断网的情况 由于原本连接正 ...

  7. maven项目在eclipse启动报错:java.lang.ClassNotFoundException

    问题: 用eclipse创建maven项目的时候,pom.xml中相关的依赖jar已经导入进去,还会报java.lang.ClassNotFoundException的错误,提示找不到相关的jar包, ...

  8. BERT和ULMFIT embedding比较文本分类结果

    Instructions [THIS REPOSITORY IS UNDER DEVELOPMENT AND MOER DATASETS AND MODELS WILL BE ADDED] [FEEL ...

  9. jenkins发送测试报告邮件

     1.安装插件 Email Extension Plugin 2.设置Extended E-mail Notification a."系统管理"--“系统设置”.配置Extende ...

  10. 网络基础 08_NAT

    1 NAT的基本概念 为什么需要NAT IPv4地址紧缺 什么是NAT NAT(Network Address Translation) 私有IPv4地址 10.0.0.0 - 10.255.255. ...