René Nyffenegger's collection of things on the web | |
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
Requesting XML from server [XMLHTTP] | ||
The XmlHttpRequest object is probably not optimally named. It's basically making an HTTP
request, and if the response happens to be XML, there are some convenience functions
to get a fully parsed DOM of the answer.
The response, however, can be anything else (plain text, JavaScript, Perl, HTML, etc.) and you can do what you
want with it, including calling eval() on it from a JavaScript script. You can
do synchronous (blocking) or asynchronous (non-blocking) calls to your web
server, and either be notified of completion by a callback for non-blocking
calls, or just treat it like a function call for blocking calls.
The following script gives an idea on how to use XMLHTTP (within explorer!).
<script language="JavaScript"> function getPage() { var strReply = new String; var objHTTP = new ActiveXObject("microsoft.XMLHTTP") objHTTP.open('GET', 'http://abc.def.qq/foo/bar.xml', false); objHTTP.send(); /* The following fields can be used: objHTTP.status; objHTTP.statusText; objHTTP.responseText; objHTTP.responseXML; objHTTP.responseXML.nodeName; */ } </script>
Here's a more elaborate version:
if (window.netscape && netscape.security && netscape.security.PrivilegeManager.enablePrivilege) { netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead UniversalBrowserWrite'); } var url = 'http://de.yahoo.com/'; var httpRequest; if (window.XMLHttpRequest) { httpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { httpRequest = new ActiveXObject('Msxml2.XMLHTTP'); } httpRequest.open('GET', url, true); httpRequest.onreadystatechange = function (evt) { if (httpRequest.readyState == 4) { alert(httpRequest.responseText); } }; httpRequest.send(null); Links |