0

I am trying to make an API call to Openweathermap using their API+key. I am unable to parse the data to a CSS ID using $.getJSON in Javascript.

This is the jsfiddle: https://jsfiddle.net/n1Lz3vf0/

Code:

var weatherData = "http://api.openweathermap.org/data/2.5/weather? 
q=Endicott,us&appid=API+KEY";

$.getJSON(weatherData, function(data){
var town = data.name;

document.getElementById('town').innerHTML = town;
});

And it outputs to a simple div tag

Obviously the end result will be far more involved and I will be parsing much more data, but in the jsfiddle, it should simply output my city name but it's not.

3
  • 2
    The JSFiddle link is HTTPS, and your code attempts to call a HTTP endpoint. Using OpenWeatherMap via HTTPS or JSFiddle via HTTP will work (or both). Commented Dec 21, 2018 at 16:03
  • My friend, that worked. Thank you. How did you know that was the problem? Commented Dec 21, 2018 at 16:04
  • 1
    If you open the console in the JSFiddle tab, you can see the following error: Mixed Content: The page at 'https://jsfiddle.net/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint. This request has been blocked; the content must be served over HTTPS. Commented Dec 21, 2018 at 16:05

1 Answer 1

1

You have a mixed content error on the jsfiddle page, because it's a https website and you're trying to call a http url in your API call. You can't call an external API using http if you're over https, the request is blocked.

I tried your request with https and it works just as expected.

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

1 Comment

Thank you, like the other comment said, this was the problem. I was tearing my hair out.

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.