0

I'm getting this error when trying to loop over some JSON data: " Uncaught SyntaxError: Unexpected token : "

I can visit the .json file successfully, but I'm not able to access the content with the code I'm currently using. Json file is here: https://data.nba.com/data/v2015/json/mobile_teams/nba/2017/teams/pelicans_schedule_02.json

$.ajax({
type:"GET",
url:"https://data.nba.com/data/v2015/json/mobile_teams/nba/2017/teams/pelicans_schedule_02.json",
success: function(data) {

  for (var i=0; i < data.gscd.g.length; i++) {
     console.log(data.gscd.g.gid);
  } 
},
dataType: 'jsonp', 
});  

I've created a JSFiddle that shows a proper request via a separate API URL format. Am I missing something super simple here?

https://jsfiddle.net/6L85kocr/

4ndy
  • 446
  • 7
  • 26
  • The response is JSON, not JSONP, and they are not interchangable. *However* if you change to `json` then you get this error: `The 'Access-Control-Allow-Origin' header contains the invalid value ''` (https://jsfiddle.net/6L85kocr/2/) as the CORS headers are not set for the domain you're calling from. In other words, you cannot make a cross-domain request to this URL because of the Same Origin Policy. You'll have to make the request server side instead – Rory McCrossan Nov 01 '17 at 15:18

1 Answers1

0

You gave jsonp as a dataType, but you're requesting and getting JSON. They're not the same.

Seems that you either need to request dataType: json and provide the necessary access key if it's not publicly available, or you need to change the URL to request JSONP if the server makes it available.

llama
  • 2,535
  • 12
  • 11
  • Yeah I was using jsonp because Access-Control-Allow-Origin header isn't present. It is working properly with the listing of videos, but not for schedule information. Schedule API URL: http://data.nba.com/data/v2015/json/mobile_teams/nba/2017/teams/pelicans_schedule_02.json Videos API URL: https://www.nba.com/pelicans/api/1.1/json/?type=video&schedule=20171030/ORLNOP – 4ndy Nov 01 '17 at 15:20
  • With CORS, you don't need JSONP. That's the point of it. Without CORS, then if the server makes JSONP available, then you can use that, otherwise it's apparently not meant to be available for XHR requests. – llama Nov 01 '17 at 15:21