Use JSON to transmit data in Ajax [ZT]

Source: Internet
Author: User
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.

Related Article

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.