I'm trying to call an API
which returns XML
data in response.
When I run that .htm file on my local machine, it gives me the following error.
And, when running it from the codepen.io
platform, it gives me the following error.
But, when I call the API from Postman or JSON Formatter, it works gives the response. When I simply paste it in the browser and hit Enter, it works there too. But not working with my code. I'm not sure what's the problem. Can anyone please help me?
Here's my code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.parseXML demo</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<p id="someElement">Hello</p>
<p id="anotherElement"></p>
<script>
$(document).ready(function () {
var api_url = 'http://lb.50g.nl/xml/29474bb065f2b30ca5eb3496f231bced.xml'
$.ajax({
url: api_url + $(this).text(),
dataType: "xml",
async: true,
method: "GET",
headers: {
"accept": "text/xml",
"Access-Control-Allow-Origin": "*"
},
crossDomain: true,
success: function (result) {
console.log(result);
}
})
});
</script>
</body>
</html>