Search code examples
javascriptjqueryapigetjson

My API Call Isn't Returning Anything?


I'm checking to see if a Twitch.com user has ever existed. When I check the API call it is returning a value in the browser, but not in the console.

$.getJSON("https://api.twitch.tv/kraken/channels/comster404", function(data2){
  // console.log(data2.status);
  console.log(data2);
});

This is the data it should get { "error": "Unprocessable Entity", "status": 422, "message": "Channel 'comster404' is not available on Twitch" }


Solution

  • You've passed a success callback but no error callback. Since an error occurred when fetching that URL, the success callback would not be called.

    You can set the callbacks for success and error with done() and fail() respectively:

    var log = document.getElementById("log");
    
    $.getJSON("https://api.twitch.tv/kraken/channels/comster404")
      .done(function(data) {
        console.log(data);
        log.innertHTML += "success!";
      })
      .fail(function(error) {
        console.log(error);
        log.innerHTML += error.responseText;
      });
    <script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
    <div id="log"></div>