I should qualify that I am new to javascript before I go on. I have experience with C# and VBA but there are some web ideas that are taking some understanding.
As a side project I am trying to pull data from the fantasy premier league API (see an example here). I have been following other stack overflow queries and the w3s online tutorial and have the base code trying to pull the data below:
function getData() {
var element = document.getElementById("data");
var url = "http://fantasy.premierleague.com/web/api/elements/1/";
var text;
$.getJSON(url, function (player) {
$.each(player, function (key, val) {
text += key + ": " + val + "<br>";
});
});
element.innerHTML = text;}
The problem is that I keep getting the same error when running this through chrome:
XMLHttpRequest cannot load http://fantasy.premierleague.com/web/api/elements/1/. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Is there something wrong with my code, or is there something regarding the API that means my code can't access it? It is possible to go the link above and see the JSON file, so if there are access restrictions for javascript code, why is this possible?
See the code snipped below for the javascript with HTML.
function getData() {
var element = document.getElementById("data");
var url = "http://fantasy.premierleague.com/web/api/elements/1/";
var text;
$.getJSON(url, function (player) {
$.each(player, function (key, val) {
text += key + ": " + val + "<br>";
});
});
element.innerHTML = text;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<title>FantasyFootballData</title>
<body>
<button onclick="getData()">Get first player data</button>
<p id="data"></p>
</body>