How do I know why a getJSON call fails? [SOLVED]

How do I know why a getJSON call fails? [SOLVED]
0

#1

Hey guys, I’ve been working on my Twitch.tv project. For some reason my getJSON call fails. I know it fails but I don’t know why. Could you guys help me with this? but more importantly, for the future, how can I debug getJSON calls? how can I know why they’re not working?

Heres my code:

$(document).ready(function(){
  var url = "https://wind-bow.gomix.me/twitch-api/streams/freecodecamp";


            $.getJSON(url, function(d) {
                console.log("success");
            }).done(function(d) {
                console.log("done");
            }).fail(function(d) {
                console.log("error");
              console.log(url);
            }).always(function(d) {
                console.log("complete");
            });
       
});

Console logs: “error” and then “complete”. Thanks in advance.


#2

Usually the console will also display what error occurred. In case you are using codepen: make sure to open the real console (not the codepen one).

In this case I see a CORS error, meaning that you don’t have access to the server. I guess you should use JSONP, but I have no experience with the fCC version of the twitch api.

EDIT:
Adding ?callback=? (jsonp thing…) to the url fixes it.


#3

Thanks a lot! it worked. I guess that with the error displayed in the real console I can google the solution to it right?


#4

Absolutely! There will be a solution to almost any problem you can encounter.


#5

Thanks. Just one more question if you dont mind. Im getting this error: net::ERR_CONNECTION_REFUSED but I don’t know why. Ive beens searching but can quite figure it out.


#6

It works fine for me (pen), are you sure it is from the API request?


#7

Yeah. It was that. Its working fine now. Thanks again.