-2

I'm trying to use OpenWeatherMap and get the JSON for the weather. In the console i see 404 message, but when i visit the url manually it gives me the correct JSON

var latitude;
var longitude;
var apiId = "c440e3f473378f9705827ed71efe5dcc";
var request = new XMLHttpRequest();

function getLocation() {

  if (navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(function getPosition(position) {
      latitude = position.coords.latitude;
      longitude = position.coords.longitude;
      getJson();
    });

  } else {
    alert('Geo location not working or not supported by your browser.');
  }

}

function getJson() {

  request.open('GET', "api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=" + apiId + "");

  request.onload = function(data) {
    console.log(data);
  };

  request.send();

}
getLocation();

3
  • You are missing a protocol in that url of yours. While we humans understand that this is a uri, and your browser will attempt to correct that your computer does not know what to do with it. prepend http:// or https:// or perhaps even www.
    – Xorifelse
    Commented Mar 25, 2017 at 6:24
  • I assume you're testing this locally on your device (and without any local server), this means you must use https:// or http:// in beginning of every link, or else it'll just assume it to be file:/// by default.
    – Saharsh
    Commented Mar 25, 2017 at 6:28
  • thanks @Saharsh I'm using codepen so i am having problems as OpenWeatherMap uses http, and it is not working Commented Mar 25, 2017 at 7:27

1 Answer 1

2

You need to specify http at the beginning of your URL

request.open('GET', "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=" + apiId + "");
2
  • thanks, but now I'm getting the error that request is blocked as request should be "https". I added it to url and the link doesn't work(I'm using Code pen) Commented Mar 25, 2017 at 6:44
  • What do you mean by "it doesn't work?" Are you getting an error?
    – AJ Funk
    Commented Mar 25, 2017 at 8:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.