Reading an XML document using AJAX
var ajax = new XMLHttpRequest();
ajax.open("GET", url, false);
ajax.send(null);
var xmlDoc = ajax.responseXML;
do something with xmlDoc;
- if the response received is in XML,
responseXML
will contain an XML DOM object
responseText
used for all other types of data
- frequent issue: MIME type received must be
application/xml
or won't be parsed as XML
- can use all DOM properties and methods on the object returned
- passing
false
makes this example synchronous — the script will pause until data is loaded
- infrequently used, but another option
document.implementation.createDocument
accepts some parameters related to XML namespaces, but we don't need them so we just pass empty strings and null