0

I am creating a weather app using jquery. The issue that I am having is in my getJSON I would like to use a variable that I have created using your current latitude and longitude coordinates. The url with my openweather api key loads the json file just fine in the browser but it does not seem to want to work in my getJSON.

Here is my javascript code to display the weather information based off your location.

var lat;
var lon;
var jsonURL;

  navigator.geolocation.getCurrentPosition(GetLocation);
function GetLocation(location) {
    lat = location.coords.latitude;
    lon = location.coords.longitude;

    jsonURL = 'api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&APPID=example&units=imperial'

}



$("#getWeather").click(function(){
  console.log(jsonURL);
    $.getJSON(jsonURL , function( data ) {
    var items = [];
    items = data;   $("#weather").text(items.main.temp).append('℉');
    });
});
1
  • 1
    Please remove any sensitive data like your APPID. Commented Feb 22, 2016 at 21:43

3 Answers 3

3

Use the developer tools in your browser. Look at the Console. Look at the Network tab. That would make it clear that your URL is resulting in a 404 error.

Your URL is missing the scheme. It needs http:// or https:// on the front.

Browsers will insert http:// automatically when you type a URL (that is missing the scheme) into the address bar.

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

Comments

0

It's possible that the api isn't set up to work with requests directly from javascript, frequently you need to write a service on the serverside to communicate with the api, and make your AJAX request to that service instead of directly to the api. Cross-domain AJAX requests are blocked by default in browsers

Comments

0

Actually as mentioned change your url to include:

    jsonURL = 'http://api.openweathermap.org/data/...'

And also make it 'JSOP' format so:

        $("#getWeather").click(function() {
          $.getJSON(jsonURL, {
              format: "jsonp"
            })
            .done(function(data) {
              console.log(data);
            }).fail(function(jqxhr, textStatus, error) {
              var err = textStatus + ", " + error;
              console.log("Request Failed: " + err);
            });
        });

1 Comment

thanks for that but it doesn't need to be jsonp in this case

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.