2017-04-22 50 views
0

我试图调用两个API。第一个是成功返回数据。第二个不是。请有人帮助从api获取数据的问题

$(function() { 
     $.getJSON('https://freegeoip.net/json/').done(function(location) { 
     $("div").html(JSON.stringify(location)); 

     $.getJSON('http://api.openweathermap.org/data/2.5/weather?lat=' + location.latitude + '&lon=' + location.longitude + '&units=imperial&appid=b3ce5b75f220eaf1db1df46a93a6595e', 
      function(data) { 
      console.log(data); 
      }); 
     }); 
    }); 
+0

你所说的 “第二个是不是” 是什么意思?你得到的错误是什么? – gaganshera

+0

您的代码在我的浏览器上按原样工作。 – gaganshera

+0

您是否试图在https域上加载它?你在控制台中遇到什么错误?因为你的代码看起来很好。 –

回答

0

问题与httphttps://codepen.io加载了https协议,但要求一个不安全的XMLHttpRequest的端点。

http://api.openweathermap.org/data/2.5/weather?lat=17.3753&lon=78.4744&units=imperial&appid=b3ce5b75f220eaf1db1df46a93a6595e

因此,这个请求已被禁止;内容必须通过HTTPS提供,或直接在应用程序中注入代码而不是通过在线工具运行。

DEMO

$(function() { 
 
     $.getJSON('https://freegeoip.net/json/').done(function(location) { 
 
     $("div").html(JSON.stringify(location)); 
 

 
     $.getJSON('http://api.openweathermap.org/data/2.5/weather?lat=' + location.latitude + '&lon=' + location.longitude + '&units=imperial&appid=b3ce5b75f220eaf1db1df46a93a6595e', 
 
      function(data) { 
 
      console.log(data); 
 
      }); 
 
     }); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div></div>