Asp.net is asynchronously submitted to a general handler and returns the json string, asp. netjson
After learning jQuery easy-ui, I made a question about $. the example of post () asynchronous submission is specially recorded to deepen your understanding of asynchronous submission. There are two flowers and one table. Below is an example I wrote.
1 <form id = "form1" action = "#"> 2 <p> comment: </p> 3 <p> name <input type = "text" name = "username" id = "username" class = "easyui-validatebox" required = "true" missingmessage = "Enter the user name"/> </p> 4 content: <textarea name = "content" id = "content" rows = "2" cols = "20" class = "easyui-validatebox" required = "true" missingmessage = "Enter the content "> </textarea> 5 <p> <input type =" button "id =" send "value =" Submit "/> </p> 6 7 </form> 8 <div class = "comment"> existing comments: </div> 9 <div id = "resText"> 10 11 </div>Front-end Page code
The backend is written by a general processing program. Due to the need to serialize the data sent from the front end, I have constructed a person object to store the data transmitted from the front end, then, serialize the person object into a json string through the JavaScriptSerializer class in c # And upload it to the foreground
The following is the code of the general processing program:
1 public void ProcessRequest (HttpContext context) 2 {3 context. response. contentType = "text/html"; 4 string username = context. request. form ["username"]; 5 string content = context. request. form ["content"]; 6 if (username! = Null & content! = Null) 7 {8 person p = new person (); 9 p. username = username; 10 p. content = content; 11 JavaScriptSerializer jss = new JavaScriptSerializer (); 12 string message = jss. serialize (p); 13 context. response. write (message); 14 15} 16 17 18} 19 20 public bool IsReusable21 {22 get23 {24 return false; 25} 26} 27} 28} 29 // <summary> 30 // construct the person class for storing data 31 // </summary> 32 public class person33 {34 public string username {get; set;} 35 public string content {get; set;} 36}Handler1
To load data to the front end and display it, parse the json object with $. parseJSON (data) on the front end and write them to <div id = "resText"> The following jquery code
$ (Function () {$ ("# send "). click (function () {var username = $ ("# username "). val (); var content = $ ("# content "). val (); if (username = "" | content = "") {$. messager. alert ("prompt", "Enter the user name or comment! "," Info ");} else {$. post ("Handler1.ashx", {username: $ ("# username "). val (), content: $ ("# content "). val ()}, function (data, textStatus) {var person = $. parseJSON (data) $ ("# resText" example .html (person. username + "<br/>" + person. content );})}});});