1

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 混合内容:“ https://codepen.io/Gochaia/pen/ZWVoYz ”页面是通过 HTTPS 加载的,但请求了不安全的 XMLHttpRequest 端点“ http://api.openweathermap.org/ ”数据/2.5/weather?lat=-0.5507657&lon=47.475692800000004&appid=c024988151d2439343bc4617141ac25d '。此请求已被阻止;内容必须通过 HTTPS 提供。

谢谢。

更新

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

4

0 回答 0