JSON implementation asynchronous request (submit comment)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.