OpenWeatherMap API HTTPS 拒绝 javascript
OpenWeatherMap API HTTPS refusal javascript
我正在参加免费代码训练营并尝试使用 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 密钥,但我只是把它藏在这里了。
我已经尝试了几种不同的方法来修复它,我在其他帖子上看到过,但到目前为止没有任何效果:/
我正在为请求使用此代码
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。
您必须代理您的请求,或者使用不同的服务。
它现在可以工作了,我认为这是因为它说的是 readystate 而不是 readyState :/
我遇到过完全相同的情况,这就是答案。
这是因为,页面 (https://codepen.io
) 是通过 https 加载的,但是请求是针对非安全来源发出的。 (http://openweathermap.org
)。所以基本上它不会在安全页面上提供非安全内容。
您有 2 个选择;
- 切换到非安全代码笔页面(
http://codepen.io/.....
)
- 从 openweathermap.org 购买 PRO 会员并将请求发送到 https://... 频道
我遇到了同样的问题。我最终通过简单地使用不安全的 HTTP 请求而不是安全的 HTTPS 请求解决了这个问题。即我将 api url 从 https://api.openweathermap.org/...
更改为 http://api.openweathermap.org/...
这里是支持代码:
不工作
function fetchWeatherInfo(){
$.ajax({
type: 'GET',
url: 'https://api.openweathermap.org/data/2.5/forecast/daily?q=Bamenda&appid=****myid',
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {},
cache: false
});
}
正在工作
function fetchWeatherInfo(){
$.ajax({
type: 'GET',
url: 'http://api.openweathermap.org/data/2.5/forecast/daily?q=Bamenda&appid=****myid',
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {},
cache: false
});
}
如果您必须使用 HTTPS,请将以下内容附加到 API 的 url
https://cors-anywhere.herokuapp.com/
以至于变成这样...
https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/forecast/daily?q=Bamenda&appid=****myid
使用它来拨打您的 API 电话,他们将被视为安全的
当我将其推送到 github 页面时,我做了同样的事情并遇到了同样的问题。问题是将 http 删除为 https。
我使用了 fetch 方法,因为它很灵活。在那里检查我的代码 https://github.com/bondarenko-vlad/weather-js
我正在参加免费代码训练营并尝试使用 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 密钥,但我只是把它藏在这里了。
我已经尝试了几种不同的方法来修复它,我在其他帖子上看到过,但到目前为止没有任何效果:/
我正在为请求使用此代码
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。
您必须代理您的请求,或者使用不同的服务。
它现在可以工作了,我认为这是因为它说的是 readystate 而不是 readyState :/
我遇到过完全相同的情况,这就是答案。
这是因为,页面 (https://codepen.io
) 是通过 https 加载的,但是请求是针对非安全来源发出的。 (http://openweathermap.org
)。所以基本上它不会在安全页面上提供非安全内容。
您有 2 个选择;
- 切换到非安全代码笔页面(
http://codepen.io/.....
) - 从 openweathermap.org 购买 PRO 会员并将请求发送到 https://... 频道
我遇到了同样的问题。我最终通过简单地使用不安全的 HTTP 请求而不是安全的 HTTPS 请求解决了这个问题。即我将 api url 从 https://api.openweathermap.org/...
更改为 http://api.openweathermap.org/...
这里是支持代码:
不工作
function fetchWeatherInfo(){
$.ajax({
type: 'GET',
url: 'https://api.openweathermap.org/data/2.5/forecast/daily?q=Bamenda&appid=****myid',
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {},
cache: false
});
}
正在工作
function fetchWeatherInfo(){
$.ajax({
type: 'GET',
url: 'http://api.openweathermap.org/data/2.5/forecast/daily?q=Bamenda&appid=****myid',
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {},
cache: false
});
}
如果您必须使用 HTTPS,请将以下内容附加到 API 的 url
https://cors-anywhere.herokuapp.com/
以至于变成这样...
https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/forecast/daily?q=Bamenda&appid=****myid
使用它来拨打您的 API 电话,他们将被视为安全的
当我将其推送到 github 页面时,我做了同样的事情并遇到了同样的问题。问题是将 http 删除为 https。 我使用了 fetch 方法,因为它很灵活。在那里检查我的代码 https://github.com/bondarenko-vlad/weather-js