Da_Brahdahud
Da_Brahdahud

Reputation: 37

Parsing an XML file with XMLHttpRequest.responseText won't display on div

I'm trying to parse a XML document from an API link....

However when I try to use the code div.innerHTML=request.responseText it won't parse the text into the div unlike it does in the alert which is called in the line before.

<div id="myDiv"></div>

<script>
var url = 'http://www.bea.gov/api/data/?&userID=XXXXX&method=GETDATASETLIST&ResultFormat=XML&';
    div = document.getElementById("myDiv");

request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
    data = request.responseText;
    alert(data);
    div.innerHTML = data;
};
request.send();
</script>

Any reason why the alert function is working but the div.innerHTML function is not working for the request.responseText?

Upvotes: 0

Views: 1868

Answers (1)

Rohith K
Rohith K

Reputation: 1492

var url = 'http://www.bea.gov/api/data/?&userID=XXXXX&method=GETDATASETLIST&ResultFormat=XML&';
var div = document.getElementById("myDiv");
request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
    data = request.responseText;
    console.log(data);
    div.textContent = data;
};
request.send();

This should work.

Upvotes: 1

Related Questions