in Default.aspx, we create two objects through JavaScript, each with the properties of name and age, and then plug the two objects into an array in the form of an array. using the Ajax built-in $.ajax API, we can set a few properties such as url,type,data,sucess, which should be noted when we want to pass the data in JSON format, we can use the json.stringify () method to convert the array data you want to transmit into a JSON format first.
1 <%@ Page Language="C #"AutoEventWireup="true"CodeFile="Json.aspx.cs"Inherits="JSON" %>2 3 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">4 <HTMLxmlns= "http://www.w3.org/1999/xhtml">5 <Headrunat= "Server">6 <title></title>7 <Scripttype= "Text/ecmascript"src= "Sitefiles/menu/js/jquery-1.9.1.min.js"></Script>8 <Scripttype= "Text/javascript">9 Ten varData='{"MenuID": "02001", "MenuName": "Fried Yellow croaker", "Menuprice": "28.00", "OriginalPrice": "", "Menuunit": "part", "ClassID": "02", "Ordernum": "1"}' One $.ajax ({ A Type:"Post", - URL:"orderjson.ashx", - datatype:"HTML", the Data:data, - Success:function(data) { - alert (data); - } + }); - </Script> + </Head> A <Body> at <formID= "Form1"runat= "Server"> - <Div> - </Div> - </form> - </Body> - </HTML>
<%@ WebHandler language="C #"class="Orderjson"%>usingSystem;usingsystem.web;usingSystem.IO; Public classOrderjson:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; stringStrjson =string. Empty; using(StreamReader reader =NewStreamReader (context. Request.inputstream)) {Strjson=Reader. ReadToEnd (); } context. Response.Write (Strjson); } Public BOOLisreusable {Get { return false; } }}