2017-07-16 132 views
0

我確定這是我在閱讀API文檔時錯過的東西 - 我試圖在jquery中硬編碼請求來調用openweather API,因爲該頁面是地理特定的:Openweather API拒絕連接

console.log('Connected to Script.js'); 
$(document).ready(weatherSettings); 
function weatherSettings() { 
    var config = 
    { 
    url: 'https://api.openweathermap.org/forecast', 
    data: { 
     id:'7281804', 
     appid: 'MYID' 
    } 
    }; 
    $.ajax(config).done(displayMelbourneData); 
    function displayMelbourneData(callback) { 
    console.log(callback); 
    } 
} 

但是我得到的錯誤是:淨:: ERR_CONNECTION_REFUSED,我不能爲我的生活弄清楚爲什麼。

任何幫助,將不勝感激。

回答

0

只要將您當前的網址更改爲http://api.openweathermap.org/data/2.5/forecast即可,您應該沒問題。

您可以嘗試直接試試這個在您的瀏覽器(記得自己的API密鑰改變YOURAPIKEY)

http://api.openweathermap.org/data/2.5/forecast?id=7281804&appid=YOURAPIKEY

下面是我嘗試和工作的樣本。順便說一下,不要忘記用您自己的API密鑰來更改YOURAPIKEY。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title></title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
</head> 
<body> 
    <script> 

    console.log('Connected to Script.js'); 
    $(document).ready(weatherSettings); 
    function weatherSettings() { 
     var config = 
     { 
     url: 'http://api.openweathermap.org/data/2.5/forecast', 
     data: { 
      id:'7281804', 
      appid: 'YOURAPIKEY' 
     } 
     }; 
     $.ajax(config).done(displayMelbourneData); 
     function displayMelbourneData(callback) { 
     console.log(callback); 
     } 
    } 
    </script> 
</body> 
</html>