Ajax JSON Application

Source: Internet
Author: User

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> ")}}});}
Related Article

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.