Asynchronous request (submit comments) in Json format and comments in json format

Source: Internet
Author: User
Tags jquery library

Asynchronous request (submit comments) in Json format and comments in json format

Paste the code and read the code to understand the relevant logic.

Html code:

<Form id = "form1" runat = "server"> <p> comment: </p> <p> Name: <input type = "text" name = "username" id = "username1"/> </p> <p> content: <textarea name = "content" id = "content" rows = "2" cols = "20"> </textarea> </p> <input type = "button "id =" send "value =" Submit "/> </p> </form> <div class =" comment "> existing comments: </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> // 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 = "<div class = 'comment'> 

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, object> (); // string jsonStr = "{}"; // create a 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.



How does ajax Implement Asynchronous form submission?

<Input type = "text" name = "username" id = "username">
<Input type = "button" value = "test" onclick = "test ();">
<Script type = "text/javascript">
Get $. ajax also has these two methods. I used a post as an example.
Function test ()
{
// This Is A jquer request. You need to load a jquery library. The example is a post request.
$. Post (
'Here is the address you requested ',
{
// The parameter you pass transmits the value of your input box as post
Username: $ ('# username'). val ()
},
Function (data)
{
Here, the specific data returned from the address you requested is determined by your own needs.
})
}
</Script>

How does liger ui assign a value to a global variable for asynchronous json data retrieval?

Var data; // assign a value to this global variable
$. Ajax ({
Type: 'post ',
Cache: false,
Url: 'scoredeal/score_info! DoEnterScoreInf. action ',
Success: function (data2 ){

Data = data2;
Alert (data2); // The json string has been obtained.
Return;
}

});

Try it. I guess it's not verified.

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.