JavaScript AJAX Basics (retiring) AJAX and APIs Call the jQuery $.getJSON method

Jake Goldstein
Jake Goldstein
1,304 Points

What is the issue with this getJSON request

Honestly for the life of me can't figure out what is wrong with this code and why I am getting an error. I defined the url, data, and the callback function and simply call getJSON with the params, still get a Bummer so can someone explain what I am doing wrong please

weather.js
$(document).ready(function() {

  var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather';
  var data = {
    q : "Portland,OR",
    units : "metric"
  };
  function showWeather(weatherReport) {
    $('#temperature').text(weatherReport.main.temp);
  }

  $.getJSON(weatherAPI, data, showWeather())
});
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>What's the Weather Like?</title>
  <script src="jquery.js"></script>
  <script src="weather.js"></script>
</head>
<body>
  <div id="main">
    <h1>Current temperature: <span id="temperature"></span>&deg;</h1>
  </div>
</body>
</html>

1 Answer

Brendan Whiting
MOD
Brendan Whiting
Front End Web Development Treehouse Moderator 84,109 Points

What you need to do is pass the reference to the function, but not call it:

$.getJSON(weatherAPI, data, showWeather) // no parens ()

The 3rd argument here needs to be a function, which will jQuery will call later, we're not calling it yet. It's the function that will be called when the response comes back.

Jake Goldstein
Jake Goldstein
1,304 Points

perfect, thanks man obvious mistake that I wont overlook now appreciate it!