Ajax asynchronously reads RSS documents

Source: Internet
Author: User
RSS is an XML-based file standard. XML files that comply with RSS specifications can be used to share content between websites. Ajax is

RSS is an XML-based file standard. XML files that comply with RSS specifications can be used to share content between websites. Ajax is short for Asynchronous JavaScript and XML. Through Ajax technology, you can send requests to a server through Hypertext Transfer Protocol (Http) and continue to process other data while waiting for the response. Using Ajax technology, you can easily read remote XML files. Therefore, you can use Ajax technology to remotely access abstract information generated based on RSS standards. You can even write an RSS reader by yourself.

Ajax is not a new language or technology. It is actually a combination of several technologies in a certain way. They play their respective roles in collaboration, including: Standardized Rendering using XHTML and CSS; Dynamic Display and Interaction Using DOM; data exchange and processing using XML and XSLT; use XMLHttpRequest for Asynchronous Data Reading. Finally, use JavaScript to bind and process all data. Well, I will not talk much about the theory. Let's look at the code.

Create an XMLHttpRequest object and send the request to the server:

1234567891011 FunctioncreateXHR (url) {if (window. XMLHttpRequest) {xmlHttp = newXMLHttpRequest ();} else {xmlHttp = newActiveXObject ("Microsoft. XMLHTTP ");} xmlHttp. open ("post", url, "false"); xmlHttp. onreadystatechange = getResponse; xmlHttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded"); xmlHttp. send (null );}

Use the DOM operation to traverse the Rss document and obtain the required values:

123456789101112131415161718 FunctionreadDoc (doc) {root = doc. getElementsByTagName ("channel") [0]; docTitle = root. getElementsByTagName ("title") [0]; docLink = root. getElementsByTagName ("link") [0]; docDescription = root. getElementsByTagName ("description") [0]; items = root. getElementsByTagName ("item"); for (vari = 0; I "+ ItemDescription. firstChild. nodeValue +"

"; Document. getElementById ("readRss "). style. display = "none"; document. getElementById ("printRss "). getElementsByTagName ("span") [0]. style. display = "none"; document. getElementById ("printRss "). innerHTML = document. getElementById ("printRss "). innerHTML + temp ;}}

Call the createXHR (url) function, input parameters, and send the following request to the server:

1 CreateXHR ("http://www.apple.com.cn/hotnews/rss/hotnews.rss ");

Response:

1234567891011 FunctiongetResponse () {if (xmlHttp. readyState = 4) {if (xmlHttp. status = 200) {rssDoc = xmlHttp. responseXML; readDoc (rssDoc); // call the readDoc () function} else {document. getElementById ("rssTitle "). innerHTML = "An error occurred while reading! "; // Alert (xmlHttp. status );}}}

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.