Reputation: 633
I am somewhat new to using fetch() and am having trouble retrieving data from an API.
function getStats() {
var url = "https://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2017-18&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight=";
var api = url;
fetch(api)
.then(function(response){
return response.json();
})
.then(function(data){
console.log(data);
})
.catch(function(error){
console.log("There is an error.", error);
})
}
The console is logging --> "There is an error." [object Error] {}
I have tried this approach with two other JSON APIs and it has worked fine, just curious if I am missing something with this specific API.
Thanks.
Upvotes: 0
Views: 1473
Reputation: 328
Try using jsonp instead, or maybe using the mode 'no-cors' with fetch:
fetch(api , {
mode: 'no-cors' // 'cors' by default
})
.then(function(response){
return response.json();
})
Upvotes: 0
Reputation: 1333
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource
It looks like this API is locked down. You may have to look in the API documentation to learn how you can access this API with a script on a separate domain.
According to this, you may only be able to use this API server-side.
Upvotes: 1