Small Example of ASP. NET handler + jquery/JSON

Source: Internet
Author: User

handelr: jsontest. ashx

Using system; <br/> using system. collections. generic; <br/> using system. web; <br/> using system. web. script. serialization; <br/> namespace mejsontest <br/>{< br/> class itemjsontest <br/>{< br/> Public int ID {Get; set ;} <br/> Public String tname {Get; Set ;}< br/>}< br/> public class jsontest: ihttphandler <br/>{< br/> Public void processrequest (httpcontext context) <br/>{< br/> context. response. contenttype = "application/JSON"; <br/> List <itemjsontest> tlist = new list <itemjsontest> (); <br/> tlist. add (New itemjsontest {id = 1, tname = "1 1 1 1"}); <br/> tlist. add (New itemjsontest {id = 2, tname = "feature 1 complex 1"}); <br/> tlist. add (New itemjsontest {id = 3, tname = "English"}); <br/> javascriptserializer serializer = new javascriptserializer (); <br/> string jsonstr = serializer. serialize (tlist); <br/> context. response. write (jsonstr); <br/>}< br/> Public bool isreusable <br/>{< br/> Get <br/>{< br/> return false; <br/>}< br/>}

 

Test. aspx

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <title> JSON test </title> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312; "/> <br/> <MCE: Script Type =" text/JavaScript "mce_src =" jquery-1.3.2.min.js "> </MCE: SCRIPT> <br/> <MCE: Script Type = "text/ JavaScript "> <! -- <Br/> $ (document ). ready (function () {<br/> $ ("# button1 "). click (function () {<br/> $. ajaxsetup ({cache: false}); <br/>$. getjson ("jsontest. ashx "," ", function (data) {<br/> var dvalue =" "; <br/> $. each (data, function (I, item) {<br/> dvalue + = "ID:" + item. ID + ", tname:" + item. tname + "<br/>"; <br/>}); <br/>$ ("# div1" ).html (dvalue); <br/> }); <br/>}); <br/> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <Form ID = "form1"> <br/> <button id = "button1"> button1 </button> <br/> <Div id = "div1"> </div> <br/> </form> <br/> </body> <br /> </ptml> 

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.