Using only JavaScript:
If you try to access the WebService URL using XMLHttpRequest, you may encounter CORS issues:
XMLHttpRequest cannot load
.
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access.
However, there is a workaround using the service.
In this case, you would request: "". This can be easily implemented in JavaScript without the need for jQuery.
A demonstration has been prepared showcasing how to achieve this:
This demonstrates:
(function() {
var newXHR;
// Function to make XMLHttpRequest calls without relying on jQuery or AngularJS $http service.
function sendXHR(options) {
// (Modern browsers) OR (Internet Explorer 5 or 6).
newXHR = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP");
if (options.sendJSON === true) {
options.contentType = "application/json; charset=utf-8";
options.data = JSON.stringify(options.data);
} else {
options.contentType = "application/x-www-form-urlencoded";
}
newXHR.open(options.type, options.url, options.async || true);
newXHR.setRequestHeader("Content-Type", options.contentType);
newXHR.send((options.type == "POST") ? options.data : null);
newXHR.onreadystatechange = options.callback;
return newXHR;
}
// Making a call to the WebService utilizing the helper function.
sendXHR({
type: "GET",
url: "https://crossorigin.me/http://finance.yahoo.com/webservice/v1/symbols/%5EIXIC/quote?format=json&view=detail",
callback: function() {
if (newXHR.readyState === 4 && newXHR.status === 200) {
var data = JSON.parse(newXHR.response);
var table = document.getElementById("table");
var html = "<table><thead>";
html += "<th>Name</th><th>Price</th>";
html += "</thead>";
html += "<tbody>";
for (i = 0; i < data.list.resources.length; i++) {
html += "<tr><td>" + data.list.resources[i].resource.fields.name + "</td><td>" + data.list.resources[i].resource.fields.price + "</td></tr>";
}
html += "</tbody></table>";
table.innerHTML = html;
}
}
});
})();
#table table {
border: solid 1px #CCCCCC;
border-collapse: collapse;
}
#table table td {
border: solid 1px #CCCCCC;
padding: 5px;
}
<div id="table"></div>
For a shorter version of this solution, view the following link:
https://jsfiddle.net/dannyjhonston/9okhpebk/