<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;
}
}
}