0

I'm working on a FreeCodeCamp project that involves making a JSONP call to the Twitchtv API(https://www.freecodecamp.com/challenges/use-the-twitchtv-json-api), and thought I've tried to make my code as simple and straightforward as possible:

       var members =["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp",    "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
    var memberData = [];




  $.getJSON('https://api.twitch.tv/kraken/streams/freecodecamp?callback=?',    function(data) {
   console.log(data);
 });

It does not print anything. As far as I understand, the "?callback=?" portion of getJSON call makes it a JSONP request, so that shouldn't be an issue. The array of "members" isn't really in use right now, but I included it on the off chance that it's involved with the error. I'm not really sure what the issue is, any help is appreciated.

eversomber
  • 31
  • 1
  • 6
  • Plz post any errors you see. The way jsonp works is that it pads some javascript function syntax around the json string that is returned. Callback is the name of the function that will be returned. I would guess that it doesnt like "?" as the name of a function. Try using something like ?callback=getData – Chris Newman May 27 '16 at 19:12
  • After doing a little research, it turns out you were right about "?callback=?", at least in this instance because you are using jquery. Based on this post http://stackoverflow.com/questions/11916780/changing-getjson-to-jsonp you might just try wrapping your code in a document.ready function – Chris Newman May 27 '16 at 19:43
  • `$(document).ready(function() { var members =["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]; var memberData = []; $.getJSON('https://api.twitch.tv/kraken/streams/freecodecamp?callback=?', function(data) { console.log(data); }); })` – eversomber May 27 '16 at 19:50
  • Wrapping it in a document.ready function didn't seem to change anything. I'm not getting any errors, it just won't print anything. I'll try to look a bit more thoroughly at the post you shared. – eversomber May 27 '16 at 19:51
  • Try adding a console.log statements before and after the $.getJson call to see if they even get executed. Also change the one you already have to console.log("data: " + data); – Chris Newman May 27 '16 at 19:54
  • OK, I added a console.log before getJSON and it's not showing up, so it could be an issue with console.log itself. I'm using CodePen, could that be relevant? – eversomber May 27 '16 at 20:11
  • `$(document).ready(function() { var members =["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]; var memberData = []; console.log("test"); $.getJSON('https://api.twitch.tv/kraken/streams/freecodecamp?callback=?', function(data) { alert("data:" + data); }); })` – eversomber May 27 '16 at 20:12
  • Using CodePen could definitely have something to do with it. Ive never used CodePen, so im not sure if it automatically includes jquery for you. If that is the case, you would get compilation errors which would explain none of the console.log statements executing. – Chris Newman May 27 '16 at 20:27

1 Answers1

0

On the FreeCodeCamp forum you can find many informations, but I can tell you what I already know:

  • to see the errors on codepen it's better to use the browser console: you can't get the errors information on the codepen console (at least not using its default options).
  • You can't use the url "h77ps://api.twitch.tv/kraken" without having a key (details here : https://blog.twitch.tv/client-id-required-for-kraken-api-calls-afbb8e95f843). You can use instead this workaround "https://wind-bow.gomix.me/twitch-api".
  • last thing: I was using jQuery like you to do the project, and it worked, but it's not working anymore; I don't know exactly why, but I know I was having trouble with its pure json request. Now I fixed it using Ajax and explicitly asking for "dataType : 'jsonp'"
Daniele Cux
  • 348
  • 2
  • 12