Show the local weather - why get json is not working?

Show the local weather - why get json is not working?
0

#1

Here is my JS code:

$(document).ready(function() {

  $.getJSON( "https://api.darksky.net/forecast/b4cdd18ad362908cdc21ff0b9fd58c18/0,0", function( data ) {
      $("body").html(JSON.stringify(data));
  });

});

I tried to use openweather, but no success.
I know already about html/htmls problem on codepen.io
Some other APIs work fine when i call them.

Can anyone help?

(edited to give more info)


#2

Could you please post your project on CodePen or Github?


#3

Right, sorry.

http://codepen.io/MyTrueName/pen/OpyzdM

I am quite frustrated, because it was working few days ago, so i submitted it as complete.
Then i just came back to review and - bad luck.
Took everything apart to find where was the problem, ad there it is - get json not getting json.

Thanks if you can give any help/advice.


#4

The Darksky API doesn’t allow direct request from the client:
https://darksky.net/dev/docs/faq#cross-origin


#5

The browser’s console will give you a helpful error text:

The problem is that DarkSky delivers its data with a header that says, in effect, “here are the domains that can access this response”. The error says that CodePen isn’t on that access list. DarkSky did, in fact, work at one point, but they’ve since changed their policies (I wonder if all of the FCC students using their free tier had something to do with this). It sounds fishy, but in actuality accessing DarkSky directly from the client is a horrible idea and they’re not wrong in trying to deter it. Services like this are actually meant to be accessed on a server and then delivered to clients in a way that’s easier to control. This is one reason the coming curriculum refresh is going to de-emphasize AJAX.

But what do we do about this now?

Use cors-anywhere. Drop this in front of your URL and you’ll get your data again.

"https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/..."

#6

Good to hear that it will be dropped.

I found cause of my woes - noscript.

Even though i allowed everything on codepen, it was blocking scripts from other sites.
…and did not show that they are blocked.

Thank You for help, cors is doing fine.

I hope i’ll finish this in another hour and move on with my life.

Getting unstuck is quite fun.