GET 请求 openweather API 不工作

GET request for openweather API not working

我正在制作一个使用地理定位和搜索的天气应用。

地理定位功能正常,但用户搜索失败;控制台中的错误是:GET {api url} 404 (Not Found).

首先,不起作用的功能:

    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);
       });
    });
  };

现在可以使用的功能:

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);
          });
        })
      })

    }

我似乎找不到区别,为什么一个可以工作而另一个不能。欢迎提出任何建议!

正确的URL应该是:

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

location 后面的 = 符号要求另一个值并告诉 ajax 请求 location 将是一个参数。