How to Send and Receive XML Data to and from the Server

In addition to the responseText property, XMLHttpRequest objects also have a responseXML property, which holds the data returned from the server as an XML document object (or null if the body content returned is not well-formed XML). Follow these eight steps to send XML data in an HTTP POST, receive an XML document from a server, and output the XML that was received.

  1. Create an XMLHttpRequest object and make a request for an XML document.
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST","books");
            
  2. Initialize a variable to hold the result of the AJAX request.
    var xmlDoc;
            
  3. Write code to listen for a change in the state of the xmlhttp response, using the onreadystatechange event handler.
    xmlhttp.onreadystatechange = function() {
        //Do something here
    };
                
  4. Write a callback function to be called when the state changes, and use an if statement to check whether the request was successful.
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
         //Request was successful
        }
    };
                
  5. If the request was successful, assign the XML response to the xmlDoc variable.
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            xmlDoc = xmlhttp.responseXML;
        }
    };
            
  6. If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML.
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            
  7. Use the send() method to send the request, along with any XML data.
    var xml = "<xml><query><author>John Steinbeck</author></query></xml>";
    xmlhttp.send(xml);
                
  8. Output the data to see the results. Here's a complete demo that will send XML to the server, receive XML from the server, and write the XML response to the browser's JavaScript console.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>test handle response</title>
        <script>
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("POST","books");
            var xmlDoc;
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                xmlDoc = xmlhttp.responseXML;
                console.log(xmlDoc);
                }
            };
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            var xml = "<?xml version='1.0'?><query><author>John Steinbeck</author></query>";
            xmlhttp.send(xml);
        </script>
    </head>
    <body>
    
    </body>
    </html>
            
    Outputting XML Data from Server
Author: Chris Minnick

Chris Minnick, the co-founder of WatzThis?, has overseen the development of hundreds of web and mobile projects for customers from small businesses to some of the world’s largest companies. A prolific writer, Chris has authored and co-authored books and articles on a wide range of Internet-related topics including HTML, CSS, mobile apps, e-commerce, e-business, Web design, XML, and application servers. His published books include Adventures in Coding, JavaScript For Kids For Dummies, Writing Computer Code, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW E-Commerce Designer Certification Bible, and XHTML.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.