I am just learning AJAX and trying out the same simple example that is here.
I have created a folder called ajax-first-try
and created a html file called test.html
. The following are the contents of the html.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>The page loads first</h1>
<h2>If you click the below button i will load the data in an xml without reloading the page.</h2>
<button onclick="loadTable()">click</button>
<div id="demo"></div>
</body>
<script type="text/javascript">
function loadTable() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
printTable(this);
}
};
xmlhttp.open("GET", "cd_catalog.xml", true);
xmlhttp.send();
}
function printTable(xmlhttp) {
var table = "<tr><th>Title</th><th>Artist</th><th>Country</th><th>Company</th></tr>";
<!-- code that adds rows to table -->
document.getElementById("demo").innerHTML = table;
}
</script>
</html>
The same folder contains the xml called cd_catalog.xml
whose contents are this.
When I click the button, I get the following error. I understand that this is a standard error, and there are already many questions about this error. But I am not able to understand them as they are complex use case/solution.
test.html:21 Access to XMLHttpRequest at 'file:///Users/arun-8076/Desktop/javascript/ajax-first-try/cd_catalog.xml' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
loadTable @ test.html:21
onclick @ test.html:9