Asp.net does not need to submit any new requests. JSON and ashx are used.

Source: Internet
Author: User

<SCRIPT type = "text/JavaScript">
/* Serialize the JSON object to a string */
Function serialize (OBJ ){
Switch (obj. constructor ){
Case object:
VaR STR = "{";
For (VAR o in OBJ ){
STR + = O + ":" + serialize (OBJ [O]) + ",";
}
If (Str. substr (Str. Length-1) = ",")
STR = Str. substr (0, str. Length-1 );
Return STR + "}";
Break;
Case array:
VaR STR = "[";
For (VAR o in OBJ ){
STR + = serialize (OBJ [O]) + ",";
}
If (Str. substr (Str. Length-1) = ",")
STR = Str. substr (0, str. Length-1 );
Return STR + "]";
Break;
Case Boolean:
Return "\" "+ obj. tostring () + "\"";
Break;
Case Date:
Return "\" "+ obj. tostring () + "\"";
Break;
Case function:
Break;
Case number:
Return "\" "+ obj. tostring () + "\"";
Break;
Case string:
Return "\" "+ obj. tostring () + "\"";
Break;
}
}
</SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Function (){
// Asynchronously add
$ ("# Btnadd"). Click (function (){
VaR grade = {No: "", name: "", remark :""};
Grade. No = $ ("# No"). Val ();
Grade. Name = $ ("# name"). Val ();
Grade. remark = $ ("# remark"). Val ();
VaR JSON = serialize (grade );
$ ("# Divtip" ).css ("display", "Block"); // display the prompt in Data Processing
$. Post ("http://www.cnblogs.com/Handle/Handler_Grade.ashx", {Action: "add", JSON: JSON}, function (data ){
$ ("# Divtip" ).css ("display", "NONE"); // hide the prompt in Data Processing
Alert (data );
});
});
});
</SCRIPT>

 

Public class handler_grade: ihttphandler
{
Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/plain ";
String action = context. Request. Params ["action"];
Switch (Action)
{
Case "add ":
Add (context); // Add a record
Break;
Case "Del ":
Del (context); // delete a record
Break;
Default:
Context. response. Write ("Incorrect command line parameter! ");
Break;
}

}
# Region custom Method
/// <Summary>
/// Add grade
/// </Summary>
Private void add (httpcontext context)
{
Try
{
Gradedal = new gradedal ();
Grade = new grade ();
VaR JSON = context. Request. Params ["JSON"];
Grade = deserializer (JSON );
Grade. guid = system. guid. newguid ();
Gradedal. Add (grade );
// Context. response. Write ("grade. No =" + grade. No );
Context. response. Write ("added successfully ");
}
Catch (exception ex)
{
Context. response. Write ("error:" + ex. Message );
}

}
/// <Summary>
/// Delete the grade record
/// </Summary>
/// <Param name = "context"> </param>
Public void del (httpcontext context)
{
...... // A few hundred words are omitted here
Context. response. Write ("success ");
}

# Endregion
/// <Summary>
/// Deserialize the JSON string to a C # object
/// </Summary>
/// <Param name = "strjson"> </param>
Private grade deserializer (string strjson)
{
Javascriptserializer serializer = new javascriptserializer ();
Grade = (grade) serializer. deserialize (strjson, typeof (grade ));
Return grade;
}
Public bool isreusable
{
Get
{
Return false;
}
}
}

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.