javascript - openweather API 的 GET 请求不起作用

标签 javascript ajax openweathermap

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

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

首先,不起作用的功能:

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

    }

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

最佳答案

正确的网址应该是:

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

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

关于javascript - openweather API 的 GET 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47587849/

相关文章:

javascript - NodeJS/Waterline,需要等待多次创建结束才能响应

javascript - 为什么删除sessionStorage后仍然存在

android - 使用 OpenWeatherMap API key

javascript - 为什么 console.log 在这种情况下不起作用?开放天气 map API

javascript - 在弹出窗口中,更改范围内的文本

javascript - Three.js + 传单 = 3D map ?

jquery - 在 AJAX 内容加载后使页脚 DIV 保持底部

ajax - 将数据放入 Azure 存储时未找到 COR 规则

javascript - 如何通过 Swagger 或 Ajax 将数据发送到 Presence Insights 连接器 API?

javascript - React js中openweathermap的api key