我不能在codepen上使用openweathermap API,因为Google Chrome会阻止混合内容

时间:2016-05-05 19:11:41

标签: javascript jquery api openweathermap

API必须是HTTPS,因为codepen是HTTPS,但API不能在HTTPS上运行,因此我陷入困境,我该怎么办?

以下是代码:

    $(document).ready(function() {
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(handleGetCurrentPosition, onError);
  }
  function handleGetCurrentPosition(location) {
    console.log(location.coords.longitude + " " + location.coords.latitude);
    $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=-0.5507657&lon=47.475692800000004&appid=c024988151d2439343bc4617141ac25d", function(result) {
      console.log("City: "+result.city.name + " Weather: "+ result.list[0].weather[0].description);
    });
  }
  function onError() {
    $('body').innerHTML = "";
    $('body').innerHTML = "<h1>Sorry, there was a technical problem. Please reload the page later.";
  }
});

并且出现错误消息:

jquery.min.js:4混合内容:&#39; https://codepen.io/Gochaia/pen/ZWVoYz&#39;是通过HTTPS加载的,但是请求了一个不安全的XMLHttpRequest端点&#39; http://api.openweathermap.org/data/2.5/weather?lat=-0.5507657&lon=47.475692800000004&appid=c024988151d2439343bc4617141ac25d&#39;。此请求已被阻止;内容必须通过HTTPS提供。

感谢。

更新

我找到了一个解决方案,感谢@AlexChance。 Forecast.io API通过SSL托管,因此不再有混合内容。

0 个答案:

没有答案