zoukankan      html  css  js  c++  java
  • 使用Jquery提交Json格式的数据到Asp.net程序的另类做法

    一般我们使用Jquery提交数据到Asp.net程序都有两种方法,今天我再介绍一种方法.以下是详细描述: 1)使用Get请求的方式,将参数接在请求Url的后面.例如: http://www.google.com.hk/search?q=jquery+post 获取参数的方法:在Asp.net中可以使用Request.QueryString["q"]来获取参数. 2)使用Jquery的Form.js插件中的ajaxForm方法或者 自己提交之前构造一个Form在使用ajaxForm提交数据.例如: $(document).ready(function(){ //简单模式 $("#btnSubmit1").click(function() { $("#form1").ajaxForm(); }); //复杂模式 $("#btnSubmit1").click(function() { var options = { method: 'POST', url: 'ajaxSP.aspx', after: function(response) { alert(response.id); } }; $("("#form1").ajaxForm(options); }); }); 获取参数的方法:在Asp.net中可以使用Request["q"]来获取参数. 3)那今天我想说一下第三种做法. 就是结合jQuery+Json+Asp.net并且利用Newtonsoft.Json.Net20.dll来进行Json数据的双向转换. jQuery代码: $.ajax({ url: "AjaxSP.aspx", data: "{type:'DoOpenClickStart',id:'1001'}", type: 'post', dataType: 'json', contentType: 'application/json; charset=utf8', success: function(data) { alert("ID=" + data.ID + ";Type=" + data.Type); }, error: function(xhr) { alert("失败!"); } }); Asp.net代码: 1.准备一个Entity类. publicclass AjaxSPE { /// /// 处理类型 /// publicstring Type { get; set; } /// /// 要处理的记录ID /// publicstring ID { get; set; } } 2.Asp.net接受参数以及输出结果的核心代码: //01.接受传入的参数(记得先引用Dll:Newtonsoft.Json.Net20.dll) string requestJson =string.Empty; using(StreamReader reader =new StreamReader(Context.Request.InputStream)) { requestJson = reader.ReadToEnd(); } AjaxSPE ajax2 =JsonConvert.DeserializeObject(requestJson, typeof(AjaxSPE)) as AjaxSPE; 结果数据如下图: //02.传出结果Json数据,并输出到客户端. Response.ClearContent(); Response.ContentType ="application/json"; AjaxSPE ajax =new AjaxSPE(); ajax.Type ="dddd"; ajax.ID ="1009"; string jsonString = Jsoner.JsonConvert.SerializeObject(ajax, Jsoner.Formatting.Indented); //输出Json数据到Content Response.Write(jsonString); Response.Flush(); Response.End(); 结果如下图: 好了,就介绍到这里.最然不是什么复杂的技术研究,也有它方便的地方.
  • 相关阅读:
    centos7下配置时间同步服务器
    交换机简单配置(转)
    ubuntu16.04安装docker CE
    docker下使用DB2
    iptables之centos6版本详解
    iptables之centos6版本常用设置
    iptables介绍iptables和netfilter
    git的使用学习(九)搭建git服务器
    js 图片预览
    ps -ef | grep java 查看所有关于java的进程
  • 原文地址:https://www.cnblogs.com/gym333/p/2659050.html
Copyright © 2011-2022 走看看