Use Ajax and RSS to make a homepage news

Source: Internet
Author: User
Tags add header mysql in version client

Ajax|rss



To the maintenance of a small site to add a homepage news, browser with Ajax or Ajah from the server to get news from the asynchronous display, while providing RSS feeds for the aggregator to subscribe.






XMLHttpRequest has never touched a cell phone simulator since the last time, and found a good thing on the web search: http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest /, a cross-browser XMLHttpRequest implementation, the author in the creation of the common terms (Creative Commons License) under the release of the library, as long as you do not remove the author's name and URL in the script can be used freely, use it! Download Xmlhttprequest.js to prepare for later use.






The data can be saved in the database or simply written to a file in the server, for the sake of flexibility, I'll create a new table in MySQL in the news, so several fields: ID, title, content, time. News entry and data from the database provided to the Ajax/ajah request slightly.






The following is the client XMLHttpRequest code:




In the news read ...








The only thing worth noting here is that XMLHttpRequest default is to use UTF-8 to pass data, so your data source is best to use UTF-8 otherwise (because we have the client here has specified to use the GB2312) will be garbled, which in IE will cause JavaScript error Wrong, under FireFox but no problem, so I use try and catch to wrap up the above code, lest the test when there are errors. If you want to try it, you can use the following code as a news.php:





Echo iconv ("GB2312", "UTF-8", "This is News");


echo "This is News";


?>







If you comment out the first sentence and open the second sentence is not only to see garbled, in IE will also appear-1072896748 errors. Interestingly, ie seems to also cache the content received by XMLHttpRequest, and FireFox will not, so in the test will appear to refresh IE many times see or old data problems, you can add the following 4 sentences in the program to solve this problem:
















Header ("Expires:mon, June June 1997 05:00:00 GMT");


Header ("last-modified:"). Gmdate ("D, D M Y h:i:s"). "GMT");


Header ("Cache-control:no-cache, must-revalidate");


Header ("Pragma:no-cache");










To provide the first page news to the aggregator to subscribe to the background of the news generated RSS Feed,rss 2.0 specifications can refer to http://feedvalidator.org/docs/rss2.html, the corresponding Chinese translation version can be in http:// Www.cpcwedu.com/Document/WEBOfficial/095447158.htm find.






The RSS format is in fact one of the many dialects of XML, so also to start with, the name of the root node must be RSS, in accordance with the RSS 2.0 specification of RSS in the RSS node to have a version= "2.0" attribute. The RSS node has a child node named channel, and Channel is a channel. Channel has 3 necessary subnodes, namely title, link, and description. To blog for example, title is the name of the blog, Link is a blog URL address, and description is a blog description.


Channel can contain several item child nodes, each item in the Blog corresponds to a post, we here each item is a news. All the child nodes of the item are optional, but at least include title and description, because the news has time, so we add a pubdate node to it, the following is a code that conforms to the RSS 2.0 specification RSS file content:










  -->



Http://www.mysite.comJust for testingHi, I ' m 2ndboy. Welcome to my site!Sat, modified Sep 0:00:01 GMTIt ' s great for using.Sat, Sep, 1:23:45 GMT














Read the latest news content from the database generate RSS code here is not pasted, nothing more than a string of stitching, of course, you can also use PHP XML DOM interface to generate this RSS content. This is done by providing RSS feeds on the site.






After the RSS part I suddenly have an idea, in order to use AJAX data Browser to provide information, the server to respond to AJAX requests for a separate response processing, then why not let the browser to our previous generated RSS as a data source to display the home page news? This seems like a good idea, but RSS is an XML-formatted file, and it's clear that to do so, the client uses JavaScript to parse XML files and display them.


















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.