Json实现异步请求(提交评论)
来源:互联网 发布:淘抢火车票软件 编辑:程序博客网 时间:2024/05/01 20:31
主要将代码粘贴,通过阅读代码理解其中的相关逻辑。
html代码:
<form id="form1" runat="server"> <p> 评论:</p> <p> 姓名:<input type="text" name="username" id="username1" /></p> <p> 内容:<textarea name="content" id="content" rows="2" cols="20"></textarea></p> <p> <input type="button" id="send" value="提交" /></p> </form> <div class="comment"> 已有评论:</div> <div id="resText"> </div>
js代码:
$("#send").click(function () { $.get("doSave.ashx", {<span style="white-space:pre"></span> <span style="font-family: Arial, Helvetica, sans-serif;"></span>//调用json插件 u_name: $("#username1").val(), //json数据/值对化 u_cont: $("#content").val() }, function (data) var uName = data.username; //注:此处的username与doSave.ashx中的dic.add("username",uname)中的username相对应的 var uCont = data.content; var txtHtml = "<div class='comment'><h6>" + uName + ":</h6><p class='para'>" + uCont + "</p></div>" $("#resText").html(txtHtml); //将返回的数据添加到页面上 }, "json"); })
插件代码:
<%@ WebHandler Language="C#" Class="doSave" %>using System;using System.Web;public class doSave : IHttpHandler{ public void ProcessRequest(HttpContext context) { var dic = new System.Collections.Generic.Dictionary<string, object>(); //存储的集合 string jsonStr = "{}"; //新建字符串jsonStr context.Response.ContentType = "text/json"; //定义返回的内容类型为json string uname = context.Request.QueryString[0]; //获取请求参数中第一个参数,也可以直接使用uname string commet = context.Request.QueryString[1]; //定义字符串uname、commet为context请求查询的字符串context.Request.Params["username"];QyertStrubg:查询字符串 dic.Add("username", uname); //将字符串添加到对象中 dic.Add("content", commet); jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(dic); //序列化集合为json字符串 context.Response.Write(jsonStr); } public bool IsReusable { get { return false; } }}
此处效果即为,在输入框中输入相关文字,点击提交,下方会自动将书写的文字进行展示,无需跳转其他页面。
0 0
- Json实现异步请求(提交评论)
- Json实现异步请求(提交评论)
- ajax实现异步请求的json方式;
- 异步请求网络json
- Android--使用原生技术实现ListView(原生技术实现网络异步请求,解析json数据)
- struts2+jquery实现ajax异步提交并返回json串
- 黄聪:Jquery+php+ajax实现表单异步提交,动态添加回复评论
- 异步实现qq相册评论
- ajax异步提交 页面请求
- 实现异步提交
- AJAX实现异步提交
- json前后端传输(ajax异步提交)
- 异步请求、处理JSON对象
- 异步POST请求解析JSON
- jquery异步请求返回JSON
- (跨域)jquery.ajax提交请求并返回json数据
- QQ聊天消息展示,提交评论实现
- 实现异步请求
- 第6章、编辑框EditText与按钮Button(从零开始学Android)
- ORACLE PL/SQL编程存储过程与函数
- Apache httpd.conf 配置文件语法验证
- 图论
- 16.PHP内核探索:PHP里的opcode
- Json实现异步请求(提交评论)
- windows 下apache2.2配置php5.4.31
- 编程实现单链表的删除节点
- iOS NSLog输出的格式
- libstdc++.so.6: version `GLIBCXX_*.*.*' not found
- AMD又换老板了
- BOX2D 第一章 简介
- Permutation Sequence
- S7 200 PPI编程口读写数据