Paste the code mainly and read the code to understand the relevant logic.
HTML code:
<form id="Form1" runat= "server"> <p> Comments:</P> <p> Name:<input type="text" name= "username" id="username1 " /></P> <p> Content:<textarea name="Content" id="Content" rows="2" cols="> "</textarea></P> <p> <input type="button" id="Send" value="Commit" / ></P> </form> <div class="comment"> Comments already available:</div> <div id="ResText"> </div>
JS Code:
$("#send"). Click ( function () { $.get ("Dosave.ashx", {<span style="White-space:pre"></span> <span style="font-family:arial, Helvetica, Sans-serif;" ></span>Calling the JSON plugin U_name: $ ("#username1"). Val (),//json data/value alignment U_cont: $ ("#content"). Val () }, function (data) var uName = Data.username; Note: The username here corresponds to uname in Dic.add ("username", username) in Dosave.ashx var ucont = data.content; var txthtml = "<div class=' comment '><h6>" + UName + ":</h6><p class=' para '>" + Ucont + "</P></div>" $ ("#resText"). HTML (txthtml); Add the returned data to the page }, "JSON"); })
Plugin Code:
<%@ WebHandler language="C #"class="Dosave"%>usingSystem;usingsystem.web; Public classdosave:ihttphandler{ Public voidProcessRequest (HttpContext context) { varDIC =Newsystem.collections.generic.dictionary<string,Object> ();//Collection of storage stringJsonstr ="{}";//new string Jsonstr Context. Response.ContentType ="Text/json";//Defines the content type returned as JSON stringuname = context. request.querystring[0];//Get the first parameter in the request parameter, or you can use uname directly stringCommet = context. request.querystring[1];The string context that defines the string uname, Commet for the context request query. request.params["username"]; QYERTSTRUBG: Query string Dic. ADD ("username", uname);//Add a string to the object Dic. ADD ("Content", Commet); Jsonstr = Newtonsoft.Json.JsonConvert.SerializeObject (DIC);//Serialize collection as JSON string Context. Response.Write (JSONSTR); } Public BOOLisreusable { Get { return false; } }}
Here the effect is, enter the relevant text in the input box, click Submit, the following will automatically display the written text, no need to jump to other pages.
JSON implementation asynchronous request (submit comment)