Use jquery to transmit JSON-formatted strings that are received by the backend with the ashx file

Source: Internet
Author: User

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

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.