1. Create news. ashx
Public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; string key = commonclass. request. getrequest <string> ("key", ""); string result = ""; if (Key = "news") {result = getnews (); context. response. write (result) ;}} Public String getnews () {try {datatable dt = BLL. getdatatable ("select Top 4 [innerid], [title] From web_news where [isopen] = 1 order by updateti Me DESC "); If (! Commonclass. dtrow. checkdtisempty (DT) {stringbuilder JSON = new stringbuilder (); For (INT I = 0; I <DT. rows. count; I ++) {JSON. append ("{"); JSON. append ("innerid: '" + dt. rows [I] ["innerid"]. tostring () + "'"); JSON. append (", Title: '" + dt. rows [I] ["title"]. tostring () + "'"); JSON. append ("},");} return "[" + JSON. tostring () + "]";} else {return string. empty ;}} catch (exception ex) {applog. write (" An error occurred while obtaining news! [Exception information: "+ ex. Message +"] ", applog. logmessagetype. info); Return string. Empty ;}}
2. Page Ajax
// Obtain the news function getnews () {jquery. ajax ({type: 'get', URL: 'handler/news. ashx ', data: 'key = News', // datatype: 'json', cache: false, success: function (result) {var jsonobj = eval (result ); for (I = 0; I <jsonobj. length; I ++) {$ ("# ul_news "). append ("<li> <a href = 'news/infor? Innerid = "+ jsonobj [I]. innerid + "'target = \" _ blank \ "Title = \" "+ jsonobj [I]. title + "\"> "+ jsonobj [I]. title + "</a> </LI> ")}}});}