I should create a page that shows a catalog of albums provided in xml. From there I have to transform the catalog.xml into JSON and show it in a table with artist and with Title. The problem is that my code does not read the document because it says 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 @ Teste.html: 29 xhttp.onreadystatechange @ Test.html: 15 XMLHttpRequest.send (async) loadDoc @ Test.html: 19 onclick @ Teste.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>