0

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.

4
  • 1
    It returns an object in the same format as if you accessed the url directly: stats.nba.com/stats/…. Not sure what you are expecting to be returned but this look correct to me. Commented Nov 4, 2016 at 14:30
  • You question says it is returning different json than expected. What are you expecting, because it looks like it's returning well formed json. Is the problem that you are unsure how to access the data in the json response? Commented Nov 4, 2016 at 14:32
  • It looks like you are trying to save a jquery opperation to a string have you tried to stringify? Commented Nov 4, 2016 at 14:33
  • My Return this is what I'm getting Commented Nov 4, 2016 at 14:51

1 Answer 1

3

Your example works just fine.

$(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);
      populateTable(response.resultSets, 'CommonPlayerInfo');
      populateTable(response.resultSets, 'PlayerHeadlineStats');
    }
  })
  
};
  getPlayerGameLogs();
});

function populateTable(resultSets, setName) {
  var data = resultSets.filter(function(set){
    return set.name === setName;
  })[0];
  var headers = data.headers;
  var rowSet = data.rowSet;
  var table = $('#' + setName);
  var tr = table.append($('<tr>'));
  $(headers).each(function(){
    tr.append(tr.append($('<th>').text(this.toString())));
  });
  $(rowSet).each(function(){
    var tr = $('<tr>');
    this.forEach(function(item){
      tr.append($('<td>').text(item.toString()));
    });
    table.append(tr);
  });
}
body {
  font-family: sans-serif;
}

table {
  border: 1px solid gray;
  font-size: 11px;
  border-collapse: collapse;
}

td, th {
  border: 1px solid gray;
  padding: 2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head></head>
  <body>
    <h3>Headline Stats</h3>
    <table id="PlayerHeadlineStats"></table>
    <br />
    <h3>Player Info</h3>
    <table id="CommonPlayerInfo"></table>
  </body>
</html>

Sign up to request clarification or add additional context in comments.

5 Comments

This should be a comment. It is not an answer to the OP's question
This is the return I'm getting
@VivekPradhan, the OP's questions were as follows: Could someone explain this a bit? Is it impossible to request the JSONs from nba.com? I have answered both questions by explaining that it works and confirming that it is possible to request JSON data from nba.com. What is lacking?
I've added the .resultSets , looks way better now indeed. Thank you sir !
If it is not a hassle, How would I get only the headers and rowsets and then put it in a table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.