AJAX - The Complete Reference

(avery) #1

130 Part I: Core Ideas


xhr.setRequestHeader("Content-Type", "text/xml");
xhr.onreadystatechange = function(){handleResponse(xhr);};
xhr.send(payload);
}
}

You can see the transmission of posted XML data in the network trace here, or you can
play with it yourself and run the example at http://ajaxref.com/ch4/xmlrequest.html.

NNOT EOTE You will likely always be using the POST method in the case of passing data beyond the
standard x-www-form-urlencoded format unless you want to take the payload and then
further encode it in that format to be put into the query string.

You might rightfully wonder why we did not use DOM methods to create the XML
packet. We certainly could have, but as you’ll see, it is even more work. First, to address the
various cross-browser issues when working with the XML DOM, it is necessary to abstract
away the creation of the XML document to send. To do so, the wrapper function
createXMLDocument is created.

var xmlDoc = createXMLDocument();

Now in this function, the XML document is created either through Internet Explorer’s
ActiveX approach or the W3C syntax for XML document creation supported in other
browsers.

function createXMLDocument()
{
var xmlDoc = null;
if (window.ActiveXObject)
{
var versions = ["Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"];
Free download pdf