JSON is a technical means for JavaScript objects to interact with server objects.
Using function to build function classes, using JSON class library parsing on the. NET Server
This JavaScript function class is used to transfer data between JavaScript and the server.
It is often used in Ajax.
Request page send_json_request.aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "send_json_request.aspx.cs" inherits = "ajax_send_json_request" %>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Script language = "JavaScript" src = "JSON. js"> </SCRIPT>
<Script language = "JavaScript">
VaR XMLHTTP;
Function createxmlhttp ()
{
If (window. activexobject)
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
XMLHTTP = new XMLHttpRequest ();
}
}
Function user (name, PWD)
{
This. Name = Name;
This. Pwd = PWD;
}
Function getuser ()
{
Return new user (document. getelementbyid ('txtname'). Value, document. getelementbyid ('txtpwd'). value );
}
Function getuserinfo ()
{
VaR user1 = getuser ();
VaR json_user1 = JSON. stringify (user1 );
Createxmlhttp ();
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. Open ("Post", "send_json_response.aspx? Timestamp = "+ new date (). gettime (), true );
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHTTP. Send (json_user1 );
Return false;
}
Function handlestatechange ()
{
If (XMLHTTP. readystate = 4 & XMLHTTP. status = 200)
{< br> alert (XMLHTTP. responsetext );
}< BR >}< br>
User Name:
password:
Response page send_json_response.aspx:
Using system;
Using system. Collections. Generic;
Using system. text;
Using newtonsoft. JSON;
Using system. IO;
Using nunit. Framework;
Public partial class ajax_send_json_response: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String STR = string. empty;
Byte [] bytes = request. binaryread (request. totalbytes );
STR = system. Text. encoding. utf8.getstring (bytes );
User u = (User) javascriptconvert. deserializeobject (STR, typeof (User ));
If (U. Name. tolower (). Equals ("admin") & U. pwd. tolower (). Equals ("admin "))
{
Response. Write ("Administrator ");
}
Else if (U. Name. tolower (). Equals ("user") & U. pwd. tolower (). Equals ("user "))
{
Response. Write ("normal user ");
}
Else
{
Response. Write ("input error ");
}
}
New Class user
{
Private string name;
Private string PWD;
Public string name
{
Set {This. Name = value ;}
Get {return this. Name ;}
}
Public String pwd
{
Set {This. Pwd = value ;}
Get {return this. pwd ;}
}
}
}
Note: JSON. JS, newtonsoft. JSON. dll, nunit. Framework. dll.