0

I get the following in my console

GET http://localhost/FCC%20Projects/Show%20Local%20Weather/api.openweathermap.o…9999&lat=43.3104064&APPID=4c45bb0e6071b74cf43da0d4aa498377&_=1440245698059 404 (Not Found)

if i take out the http://localhost/FCC%20Projects/Show%20Local%20Weather/ part and paste the remaining in the browser bar, i get the proper response from the api service. I get the same problem on gh-pages except that it is prefixed with the GitHub address. http://adoyle2014.github.io/FCC-ShowLocalWeather/

function apiCall(lat, long) {
  $.ajax({
    url: "api.openweathermap.org/data/2.5/weather?",
    jsonp: "jsonp",
    dataType: "jsonp",
    data: {
      lon: long,
      lat: lat,
      APPID: apiKey
    },
    success: function (response) {
      parseWeather(response);
    }
  });

why does this api call prepend the url with the current website address?

1
  • 2
    Just make the start with http://. Longer explanation as answer coming up! Aug 22, 2015 at 12:32

3 Answers 3

5

The reason that it prepends the current URL onto the API URL is because the URL your provided does not start with http://, meaning it is a relative URI. They think that this URI is relative to the current URL, so they prepend the current URL to it and then go from there.

To fix this, simply start the URI with http://:

function apiCall(lat, long) {
    $.ajax({
        //This is our fix:
        url: "http://api.openweathermap.org/data/2.5/weather?",
        jsonp: "jsonp",
        dataType: "jsonp",
        data: {
            lon: long,
            lat: lat,
            APPID: apiKey
        },
        success: function (response) {
            parseWeather(response);
        }
    });
}

For more information on relative URIs vs absolute URLs, go to this tutorial from Indiana University.

0
2

Use either HTTP or HTTPS, otherwise it will consider as a local resource. Example:

url: "http://api.openweathermap.org/data/2.5/weather?",
0

Provide / in path. like below -

   function apiCall(lat, long) {
        $.ajax({
            url: "/api.openweathermap.org/data/2.5/weather?",
            jsonp: "jsonp",
            dataType: "jsonp",
            data: {
                lon: long,
                lat: lat,
                APPID: apiKey
            },
            success: function (response) {
                parseWeather(response);
            }
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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