简体   繁体   English

使用XMLHttpRequest.responseText解析XML文件不会显示在div上

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

I'm trying to parse a XML document from an API link.... 我正在尝试从API链接解析XML文档。

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.innerHTML = request.responseText时,它不会像在上一行中调用的警报中那样将文本解析为div。

<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? 警报功能起作用但div.innerHTML函数不适用于request.responseText的原因是什么?

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. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM