2016-09-29 140 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調用,併爲確保