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>