Asynchronous request in Json format (submit comments)
Paste the code and read the code to understand the relevant logic.
Html code:
Existing comments:
Js Code:
$ ("# Send "). click (function () {$. get ("doSave. ashx ", {// call the json plug-in u_name: $ (" # username1 "). val (), // json data/value pairs u_cont: $ ("# content "). val ()}, function (data) var uName = data. username; // Note: username and doSave here. dic in ashx. var uCont = data. content; var txtHtml = "" + uName + ":"+ UCont +"
"$ (" # ResText ").html (txtHtml); // Add the returned data to the page}," json ");})
Plug-in code:
<% @ 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 jsonStr = "{}"; // create a new string jsonStr context. response. contentType = "text/json"; // defines the returned content type as json string uname = context. request. queryString [0]; // obtain the first parameter in the request parameter. You can also directly use uname string commet = context. request. queryString [1]; // defines the string uname and commet as the string context to be queried in the context request. request. params ["username"]; QyertStrubg: query string dic. add ("username", uname); // Add the string to the object dic. add ("content", commet); jsonStr = Newtonsoft. json. jsonConvert. serializeObject (dic); // the serialization set is a json string context. response. write (jsonStr);} public bool IsReusable {get {return false ;}}}
The effect is as follows: Enter the relevant text in the input box and click Submit. The text written is displayed automatically below, so you do not need to jump to other pages.