虽然weatherapp链接正常工作,但$ .getJson失败

时间:2016-06-13 19:36:19

标签: javascript json frontend

我正在https://www.freecodecamp.com/challenges/show-the-local-weather

进行该项目

如果您需要,我的完整项目就在这里:https://codepen.io/tugrulz/pen/beEmJb?editors=0010

function fetchWeather() {

  var ap = "http://api.openweathermap.org/data/2.5/weather?";
  var key = "&appid=061f24cf3cde2f60644a8240302983f2";
  var lat = "35";
  var lon = "139";
  var api = ap + "lat=" + lat + "&lon=" + lon + key;

  $(".location").html(api);

$.getJSON(api, function(data) {
      alert("sa");
      $(".location").html("oldu mu?");
    })
   .done(function() {
    alert( "second success" );
  })
  .fail(function(error) {
    alert( error );

  })
  .always(function() {
    alert( "complete" );
  });
}

打印:http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=061f24cf3cde2f60644a8240302983f2

问题是,我打印的链接在chrome中正常工作,但getJSON不起作用。它的失败功能起作用。

我该怎么办? 任何想法如何打印错误信息?打印(误差);没有帮助。既不是涉及getJson的try和catch语句。

1 个答案:

答案 0 :(得分:0)

我运行了你的codepen项目,在我的控制台中我收到了错误

The page at 'https://codepen.io/tugrulz/pen/beEmJb?editors=0011' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=061f24cf3cde2f60644a8240302983f2'. This request has been blocked; the content must be served over HTTPS.

Codepen阻止了api,因为它不是https地址。它不会打印错误,因为请求永远不会被发送到气象服务。

相关问题