1
I should create a page that shows a catalog of albums provided in xml. Then I have to transform the catalog.xml into JSON and show it in a table with artist and title. The problem is that my code does not read the document because it says that there are invalid tags.
Error provided in Google Chrome: VM40:1 Uncaught Syntaxerror: Unexpected token < in JSON at position 0 at JSON.parse () at myFunction (Test.html:29) At Xmlhttprequest.xhttp.onreadystatechange (Test.html:15) myFunction @ Test.html:29 xhttp.onreadystatechange @ Test.html:15 Xmlhttprequest.send (async) loadDoc @Test.html:19 onclick @Test.html:5
<!DOCTYPE html>
<html>
<body>
<h2>XMLHttpRequest</h2>
<button type="button" onclick="loadDoc()">Catálogo</button>
<br><br>
<p id="tabelaId"></p>
</body>
<script>
function loadDoc(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET",
"http://clienteweb2017.000webhostapp.com/fundamentosWeb/paginaCatalogo.php",
true);
xhttp.send();
}
function myFunction(response) {
var i;
console.log (response.responseText);
var xmlDoc = response.responseText;
var table="<table border=1 style=border-collapse:'collapse'; ><tr>
<th>Artist</th><th>Title</th></tr>";
var x = JSON.parse(xmlDoc);
for (i = 0; i <x.length;i++) {
table += "<tr><td>" +
xmlDoc+"</td></tr>";
}
document.getElementById("tabelaId").innerHTML = table;
}
</script>
</html>
Linked by the answer. I was left with a question about an excerpt of the code. I would like to know what exactly this query selector does. Does he interpret the tags<> ? For example: <catalogo><album><Artist> Bob<Artist><album><catalogo> There it goes exactly in the text of the tag I choose or this is because of the text content ?
– Gabriel Silva
Oops! It’s just a dial. You can select the tag you want.
– Sam