[英]$.getJSON doesn't return data
我正在上freecodecamp上的一门课程,目前陷入了天气应用zipline的困境。 我正在调用的API是OpenWeatherMap。 对我来说,问题是$ .getJson即使链接正确也不会返回数据。 我将警报放置在$ .getJSON之外,并且工作正常。 我将分享源代码:
如果您看不到html标记,那么所有这些操作都是在codepen上完成的,这是因为codepen本身就是这样做的。 CSS也这样做。
的HTML
<head>
<title>Weather App</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<h1>Free Code Camp Zipline</h1>
<h2>Local Weather App!</h2>
<p id="latitude"></p>
<p id="longitude"></p>
<div id="weather"></div>
<footer>
<p>Copyright © Luis M. Alvarez 2016. All Rights Reserved</p>
</footer>
</body>
的CSS
body {
margin: 0;
font-family: "Georgia";
}
h1, h2 {
text-align: center;
}
p {
font-size: 20px;
text-align: center;
}
Java脚本
$(document).ready(function(){
///Geolocation
//Find the geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$("#latitude").html("latitude: " + position.coords.latitude);
$("#longitude").html("longitude: " + position.coords.longitude);
///Weather API
//Setup for weather app
var key = "d160d975b9920be65fcf14313e95afb4";
var weatherNow = "http://api.openweathermap.org/data/2.5/weather?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude + "&APPID=" + key + "&units=metric";
//Get the weather
//Here alert does work
$.getJSON(weatherNow, function(data) { //Where weatherNow is on this line the alert works too example: $.getJSON(alert(weatherNow), function(data) {
//Here the alert doesn't work
alert(weatherNow);
});
});
};
});
注释已添加到此处,因此它们不会弄乱代码。 请彻底解释为什么它不起作用以及如何使其起作用。
项目链接: https : //codepen.io/Zero720/pen/RoOwaw
如果您熟悉浏览器中的JavaScript控制台,则对JavaScript进行编程会容易得多 。 这是将显示在您的错误中:
混合内容:“ https://codepen.io/Zero720/pen/RoOwaw ”上的页面已通过HTTPS加载,但请求了不安全的XMLHttpRequest端点“ http://api.openweathermap.org/data/2.5/weather?lat = 42.9976979&lon = -77.50486959999999&APPID = d160d975b9920be65fcf14313e95afb4&units = metric '。 该请求已被阻止; 内容必须通过HTTPS提供。
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.