简体   繁体   中英

GET request for openweather API not working

I'm making a weather app that uses geolocation, but also search.

The function for geolocation is working, but user search is failing; the error in the console is: GET {api url} 404 (Not Found).

First, the function that does NOT work:

    function getWeatherWithUserInput() {
  return new Promise(function(resolve, reject) {

   var location = $("#location").val().trim();
   var widget = Mixcloud.PlayerWidget(document.getElementById('my-widget-iframe'));

   var weatherCSQueryURL = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "=&appid=" + WeatherAPIKey;

    $.ajax({
      url: weatherCSQueryURL,
      method: "GET"
     }).done(function(response) {
       //$(".city").html("<h1>" + response.name + " Weather </h1>");
       $(".wind").html("<h1>" + response.name + "</h1>");
       //$(".humidity").html("Humidity: " + response.main.humidity);
       //var f_temp = 9/5*(response.main.temp-273)+32;
       //$(".temp").html("Temperature (F) " + Math.floor(f_temp));
       resolve(response.weather[0].id);
       });
    });
  };

now the function that DOES work:

function getWeatherWithGeo() {
  return new Promise(function(resolve,reject) {
    getGeoLocationGoogle()
      .then(function(response) {
          var lat = response.location.lat;
          var lon = response.location.lng;

          var weatherLLQueryURL = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=" + WeatherAPIKey;
          $.ajax({
              url: weatherLLQueryURL,
              method: "GET"
          }).done(function(response) {
              $(".wind").html("<h1>" + response.name + "</h1>");
              resolve(response.weather[0].id);
          });
        })
      })

    }

I can't seem to find the difference, why one would work while the other doesn't. Any suggestions welcome!

The correct URL should be:

var weatherCSQueryURL = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + WeatherAPIKey;

The = sign after the location was asking for another value and telling the ajax request that location would be a parm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM