I am trying to get free NFL data from an API by suredbits. Unfortunately, in their header, they have not added Access-Control-Allow-Origin
, which creates a CORS issue. When I try and run code like this...
$.getJSON("", function(playerData) {
alert(playerdata);
}
I keep getting this error No 'Access-Control-Allow-Origin' header is present on the requested resource.
So basically, is their any way to fix their mistake through the client side?
I am trying to get free NFL data from an API by suredbits. Unfortunately, in their header, they have not added Access-Control-Allow-Origin
, which creates a CORS issue. When I try and run code like this...
$.getJSON("http://api.suredbits./nfl/v0/stats/jones/julio", function(playerData) {
alert(playerdata);
}
I keep getting this error No 'Access-Control-Allow-Origin' header is present on the requested resource.
So basically, is their any way to fix their mistake through the client side?
Share Improve this question asked Sep 9, 2017 at 3:24 EthernetzEthernetz 9784 gold badges16 silver badges35 bronze badges 7- 1 no, because CORS protects resources - you could proxy requests via your server, then CORS isn't an issue – Jaromanda X Commented Sep 9, 2017 at 3:28
- That's what I've e to realize after reading just about every question about this on stackoverflow. Are there any tutorials or anything that could teach me how to do that? – Ethernetz Commented Sep 9, 2017 at 3:30
- depends on your server side technology – Jaromanda X Commented Sep 9, 2017 at 3:30
- You can just make the calls serverside. I don't think this is so much a mistake by them, they simply don't want to be called by any random browser internet-wide. – pvg Commented Sep 9, 2017 at 3:31
- 1 I have to admit it is a bit strange the API wouldn't include that header. I believe the best thing to do for now is to reach out to SuredBits (twitter./suredbits) and ask if that's a mistake or if they have any alternative methods of querying the data from JS (with a token?). The only other alternative to getting this work from JS is setting up a proxy on your web server - all this means is that it is your own server getting data from the API and then returning it to your script (thus avoiding the CORS issues). – mnewelski Commented Sep 9, 2017 at 3:49
1 Answer
Reset to default 6There is actually a way to fix it from client side: by changing the request URL to https://cors-anywhere.herokuapp./http://api.suredbits./nfl/v0/stats/jones/julio
:
const proxyURL = "https://cors-anywhere.herokuapp./";
const requestURL = "http://api.suredbits./nfl/v0/stats/jones/julio";
$.getJSON(proxyURL + requestURL, function(playerData) {
console.log(playerData);
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Yeah, that’s not exactly fixing it from the client side—since what it’s actually doing is, causing the request to get made through https://cors-anywhere.herokuapp./, which is an open CORS proxy that just adds the Access-Control-Allow-Origin
response header so that browsers will allow your frontend JavaScript code to access the response.
But as demonstrated in the code snippet in the answer, it definitely doesn’t require any changes to the server hosting the API endpoint you want to get data from. Instead all it requires a trivial change to your own frontend code.
Of course relying on a public open proxy like this is not the right solution in all cases. But at least it’s a good solution in the case where the API provider just hasn’t gotten around to CORS-enabling their API so that you could send requests to it directly and get responses you can use.
The How to use a CORS proxy to get around “No Access-Control-Allow-Origin header” problems section of the answer at No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API has a few more details.