2016-09-29 141 views
0

我正在通过免费代码阵营并尝试使用OpenWeatherMap API构建天气应用程序,但它不起作用。我正在使用codepen,因为这是它需要提交的内容,它必须是使用地理位置的https。这已成为一个问题,因为我得到这个错误。OpenWeatherMap API HTTPS拒绝javascript

混合内容:在“https://s.codepen.io/boomerang/8658fc75197c1c3799d7eb446c1be54c1475174843341/index.html?editors=0010”加载页面通过HTTPS,但要求一个不安全的XMLHttpRequest端点“http://api.openweathermap.org/data/2.5/weather?lat=54.757753799999996&lon=-1.6074879&APPID=APIIDHERE”。此请求已被阻止;内容必须通过HTTPS提供。

出于某种原因,我想如果我改变API调用HTTPS但后来我得到这个错误

得到它可能工作https://api.openweathermap.org/data/2.5/weather?lat=54.757775699999996&lon=-1.6074815999999998&APPID=APIIDHERE网:: 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(); 
} 

任何帮助将不胜感激:)

+0

我写了一个解决这个(这里),他们将被视为(HTTPS ://stackoverflow.com/questions/38659464/jquery-ajax-call-with-openweathermap-neterr-connection-refused/44900102#44900102) – Pila

回答

1

尝试使用 https://pro.openweathermap.org端点。

实际上,它好像是OpenWeatherMap SSL支持isn't free
您必须代理您的请求,或使用不同的服务。

+0

403禁止:/ –

+0

这是严峻的巴哈,这是该课程推荐的API。干杯 –

+0

尝试在普通http连接上使用codepen – lauriys

0

它的工作,现在我想这是因为它说的readyState代替readyState的:/

0

我一直在完全相同的情况,这里就是答案。

这是因为,页面(https://codepen.io)是通过https加载的,但是该请求已发送到非安全源。 (http://openweathermap.org)。所以基本上它不会在安全页面上提供不安全的内容。

你有2个选项;

  1. 切换到非安全codepen页(http://codepen.io/.....
  2. 购买PRO成员从openweathermap.org并发送请求到https:// ...通道
0

我面临同样的问题。我终于通过简单地使用不安全的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 
     }); 

} 

WORKING

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

} 
0

如果必须使用的HTTPS,将以下内容添加到A的URL中PI https://cors-anywhere.herokuapp.com/,使之成为这样的事情...

https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/forecast/daily?q=Bamenda&appid=****myid

用它来使你的API调用,并为确保