【发布时间】:2017-03-22 01:51:51
【问题描述】:
我现在正在学习 freecampcode 课程,我在当地的天气应用程序中。
我的问题是每次我尝试使用时,我都没有定义 apiLink。我知道 .getJSON 部分无法与 definedCordinates 函数中定义的 var 连接。
我还尝试在 .getJSON 函数上使用直接 api 链接,但即便如此,我还是从 json 获得 0 输入,我使用 console.log(data) 和 $("#temp").text 来检查来自 json 的输入。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Local Weather App</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="custom.css">.
<script type="text/javascript">
$(document).ready(function(){
getCordinates();
$.getJSON(apiLink, function(data){
console.log(data);
})
})
function getCordinates(){
navigator.geolocation.getCurrentPosition(definedCordinates);
}
function definedCordinates(position){
var latitude = Math.floor(position.coords.latitude);
var longitude = Math.floor(position.coords.longitude);
var apiLink = "api.openweathermap.org/data/2.5/weather?lat=" + latitude +"&lon="+ longitude +"&APPID=##########";
return apilink;
}
</script>
<body>
<div class="container-fluid">
<div class="row" id="titleRow">
<div class="col-md-4 col-md-offset-4">
<h1>My Local Weather App</h1>
</div>
</div>
<div class="row" id="locationRow">
<div class="col-md-4 col-md-offset-4">
<div class=row">
<div class="col-md-6" style="background-color: grey">
<p style="text-align:center">icon</p>
</div>
<div class="col-md-6" style="background-color: yellow">
<p style="text-align:center" id="temp">temp</p>
</div>
</div>
</div>
</div>
<div class="row" id="dataRow">
<div class="col-md-6 col-md-offset-3">
<div class="row">
<div class="col-md-4" style="background-color: grey">
<p id="name"> city name</p>
</div>
<div class="col-md-4" style="background-color: yellow">
<p> sky</p>
</div>
<div class="col-md-4" style="background-color: grey">
<p> wind</p>
</div>
</div>
</div>
</div>
<div class="row" id="poweredBy">
<div class="col-md-6 col-md-offset-3">
<div class="row">
<div class="col-md-6" id="powered">
<p> Powered by openweathermap.org API </p>
</div>
<div class="col-md-6"style="background-color: yellow">
<p> button celsius or farh </p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
标签: javascript jquery json