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 );}}} |