2017-07-16 197 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>