0

i have an RegistrationResponseMessages.xml:

<messages>
  <error>
    <code id="501">Couldn't retrieve the HTML document because of server-configuration problems.</code>
    <code id="502">Server busy, site may have moved ,or you lost your dial-up Internet connection.</code>
  </error>
  <success></success>
</messages>

trying to read contents of code id 501 and 502 with javascript, but it not works.

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET", "RegistrationResponseMessages.xml", false);
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;

        document.getElementById("errorCode403").innerHTML = getElementsByTagName(501)[0].childNodes[0].nodeValue);

displaying it here:

<label id="errorCode403" style="font-weight: 600; color: red;">give some error</label>

what is my problem?

1
  • getting sometimes error: getElementsByTagName is not defined Commented Jul 18, 2013 at 6:37

1 Answer 1

1

It's ajax, you have to wait for the data to be returned, then you have to access it the right way:

var xmlhttp;

if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onload = function() {
    var xmlDoc = this.responseXML,
        value  = xmlDoc.getElementsByTagName('501')[0].childNodes[0].nodeValue;
    document.getElementById("errorCode403").innerHTML = value;
}

xmlhttp.open("GET", "RegistrationResponseMessages.xml", false);
xmlhttp.send();

Not sure about the traversal in the XML, as 501 sounds like a strange tagName ?

EDIT:

to get a list of the ID's you do this inside the onload handler:

xmlhttp.onload = function() {
    var xmlDoc = this.responseXML,

    var codes = xmlDoc.getElementsByTagName('code');
    var array = [];

    for (var i=0; i<codes.length; i++) {  
        array.push( codes[i].id );
    }

    console.log(array);
}
Sign up to request clarification or add additional context in comments.

3 Comments

No, your code is ajax, that is what XMLHttpRequest is, A synchronous J avascript A nd X ML
do i need for this peace code something else to import for AJAX works on my page? i have a simple html with javascript inside
No, you do not have to import anything, it's exactly like your code but with ann onload handler for when the call has completed ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.