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(); 结果如下图: 好了,就介绍到这里.最然不是什么复杂的技术研究,也有它方便的地方.
  • 相关阅读:
    CodeForces 734F Anton and School
    CodeForces 733F Drivers Dissatisfaction
    CodeForces 733C Epidemic in Monstropolis
    ZOJ 3498 Javabeans
    ZOJ 3497 Mistwald
    ZOJ 3495 Lego Bricks
    CodeForces 732F Tourist Reform
    CodeForces 732E Sockets
    CodeForces 731E Funny Game
    CodeForces 731D 80-th Level Archeology
  • 原文地址:https://www.cnblogs.com/gym333/p/2659050.html
Copyright © 2011-2022 走看看