gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:42:34 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com