AJAX XMLHttpRequest responseXML Example

Ajax XMLHttpRequest object able to get XML response from the server. following example is simple get XML file from the server.

AJAX XMLHttpRequest responseXML Example

Example Code

<html>
<body>
<script language="JavaScript">
function send_with_ajax(){
    document.getElementById("req_xml").innerHTML = "Process Started...";
    if (window.XMLHttpRequest || window.ActiveXObject) {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(exception) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
        } else {
            xhr = new XMLHttpRequest(); 
        }
    } else {
        alert("Your browser does not support XMLHTTP Request...!");
    }

    xhr.open("GET", "xml_file.xml", true);    // Make sure file is in same server
    xhr.send(null);

    xhr.onreadystatechange = function(){ 
        if (xhr.readyState == 4) {
            if(xhr.status == 200){
                alert(xhr.responseText);
                document.getElementById("req_xml").innerHTML = "Response State : " + xhr.readyState + "
Response Text:
" + xhr.responseText; } else { document.getElementById("req_xml").innerHTML = "Error Code: " + xhr.status + "
"Error Message: " + xhr.statusText; } } }; } </script> <button onClick="send_with_ajax()">Get XML Contain</button> <p id="req_text"></p> </body> </html>

Run it...   »

Example Result