【问题标题】:GET request for openweather API not working对 openweather API 的 GET 请求不起作用
【发布时间】:2018-05-15 05:33:58
【问题描述】:

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

地理定位功能正常,但用户搜索失败;控制台中的错误是: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);
          });
        })
      })

    }

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

【问题讨论】:

  • 与 lat 和 log 相关的错误是什么?另外我建议您从 js 中的可用方法创建 lat 和 log。
  • @FlorianSchaal 错误是“GET {API 的网址在这里} 404(未找到)”
  • @DeepakJha 第二个函数(使用 lat 和 log 的那个)工作正常,它是第一个不起作用的函数。它为 Get 请求提供 404。

标签: javascript ajax openweathermap


【解决方案1】:

正确的网址应该是:

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

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

【讨论】:

  • 好的,谢谢!他们会让我在 2 分钟内接受你的回答。
猜你喜欢
  • 2023-03-29
  • 2020-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 2016-02-12
  • 1970-01-01
相关资源
最近更新 更多