我正在参加免费代码营,并尝试使用 OpenWeatherMap API 构建一个天气应用程序,但它不起作用。我使用 codepen 因为这是它需要提交的内容,并且它必须是 https 才能使用地理位置。这已成为一个问题,因为我收到此错误。
Mixed Content: The page at 'https://s.codepen.io/boomerang/8658fc75197c1c3799d7eb446c1be54c1475174843341/index.html?editors=0010' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://api.openweathermap.org/data/2.5/weather?lat=54.757753799999996&lon=-1.6074879&APPID=APIIDHERE'. This request has been blocked; the content must be served over HTTPS.
出于某种原因,我认为如果将 API 调用更改为 HTTPS 可能会起作用,但随后出现此错误
GET https://api.openweathermap.org/data/2.5/weather?lat=54.757775699999996&lon=-1.6074815999999998&APPID=APIIDHERE net::ERR_CONNECTION_REFUSED
我使用了 API key ,但我只是将其隐藏在这里。
我尝试了几种不同的方法来修复它,我在其他帖子上看到过,但到目前为止没有任何效果:/
我正在使用此代码来请求
function updateLoc (lat, long) {
var url = "https://api.openweathermap.org/data/2.5/weather?" +
"lat=" + lat +
"&lon=" + long +
"&APPID=" + APPID;
sendRequest (url);
}
function sendRequest (url) {
var xmlhttp = new XMLHttpRequest ();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
var data = JSON.parse (xmlhttp.responseText);
var weather = {};
weather.icon = data.weather.icon;
weather.dir = data.wind.deg;
weather.wind = data.wind.speed;
weather.temp = data.main.temp;
weather.loc = data.name;
weather.hum = data.main.humidity;
update (weather);
}
};
xmlhttp.open ("GET", url, true);
xmlhttp.send ();
}
如有任何帮助,我们将不胜感激:)
最佳答案
尝试使用 https://pro.openweathermap.org
端点。
实际上,OpenWeatherMap SSL 支持似乎 isn't free .
您必须代理您的请求,或者使用不同的服务。
关于javascript - OpenWeatherMap API HTTPS 拒绝 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39778109/