Kyriediculous Kyriediculous - 1 month ago 7
Ajax Question

jQuery AJAX Request returning different JSON than expected

I'm trying to request a json file through AJAX (jQuery) from NBA.com

I tried first getting the json file but got a CORS error so I tried using jsonp

This returns me an object but the object looks like it's full of functions and methods instead of the data I requested.

I made this on codepen so you can check it out, output can be checked with developer console because document.write just returns [object Object]

here is the link to codepen: http://codepen.io/kyriediculous/pen/KNKZZL

$(document).ready(function() {

function getPlayerGameLogs() {

$.ajax ({
url: "http://stats.nba.com/stats/commonplayerinfo?LeagueID=00&PlayerID=202355&SeasonType=Regular+Season&format=jsonp",
dataType:"jsonp",
success: function(response) {
console.log(response);
}
})

};
getPlayerGameLogs();
});


Could someone explain this a bit ? Is it impossible to request the JSONs from nba.com? Specifically the game logs for every player.

Answer

Your example works just fine. This is what is being returned.

jQuery310007677493461227147_1478269747050({
   "resource":"commonplayerinfo",
   "parameters":[
      {
         "PlayerID":202355
      },
      {
         "LeagueID":"00"
      }
   ],
   "resultSets":[
      {
         "name":"CommonPlayerInfo",
         "headers":[
            "PERSON_ID",
            "FIRST_NAME",
            "LAST_NAME",
            "DISPLAY_FIRST_LAST",
            "DISPLAY_LAST_COMMA_FIRST",
            "DISPLAY_FI_LAST",
            "BIRTHDATE",
            "SCHOOL",
            "COUNTRY",
            "LAST_AFFILIATION",
            "HEIGHT",
            "WEIGHT",
            "SEASON_EXP",
            "JERSEY",
            "POSITION",
            "ROSTERSTATUS",
            "TEAM_ID",
            "TEAM_NAME",
            "TEAM_ABBREVIATION",
            "TEAM_CODE",
            "TEAM_CITY",
            "PLAYERCODE",
            "FROM_YEAR",
            "TO_YEAR",
            "DLEAGUE_FLAG",
            "GAMES_PLAYED_FLAG"
         ],
         "rowSet":[
            [
               202355,
               "Hassan",
               "Whiteside",
               "Hassan Whiteside",
               "Whiteside, Hassan",
               "H. Whiteside",
               "1989-06-13T00:00:00",
               "Marshall",
               "USA",
               "Marshall/USA",
               "7-0",
               "265",
               4,
               "21",
               "Center",
               "Active",
               1610612748,
               "Heat",
               "MIA",
               "heat",
               "Miami",
               "hassan_whiteside",
               2010,
               2016,
               "Y",
               "Y"
            ]
         ]
      },
      {
         "name":"PlayerHeadlineStats",
         "headers":[
            "PLAYER_ID",
            "PLAYER_NAME",
            "TimeFrame",
            "PTS",
            "AST",
            "REB",
            "PIE"
         ],
         "rowSet":[
            [
               202355,
               "Hassan Whiteside",
               "2016-17",
               19.8,
               0.5,
               13.8,
               0.195
            ]
         ]
      }
   ]
})

One change you could make is to select only the resultSets property.

$(document).ready(function() {

function getPlayerGameLogs() {

  $.ajax ({
    url: "http://stats.nba.com/stats/commonplayerinfo?LeagueID=00&PlayerID=202355&SeasonType=Regular+Season&format=jsonp",
    dataType:"jsonp",
    success: function(response) {
      console.log(response.resultSets);
    }
  })

};
  getPlayerGameLogs();
});