【发布时间】:2017-08-29 17:35:49
【问题描述】:
所以我最近一直在制作一个天气应用程序。 我测试了我的代码,但没有显示温度、国家和当前天气的描述。我认为问题始于我的 html 地理定位功能。我觉得该功能无法解析天气 api 数据。
我的代码在下面:
$( document ).ready(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function showPosition(position) {
var lat = "lat= " + position.coords.latitude;
var lon = "lon= " + position.coords.longitude;
getWeather(lat,lon);
}
function getWeather(lat,lon){
var urlstring = "https://fcc-weather-api.glitch.me/api/current?" + lat + "&" + lon;
$.ajax({
url : urlstring,
dataType : "jsonP",
success : function(data){
var temp = data.main.temp;
var desc = data.weather[0].description;
var country = data.sys.country;
$("#desc").html(desc);
$("#temp").html(temp);
}
})
}
var ctof = $("#c/f").click(function(){
var cel = Math.round((temp - 32) * 5 / 9);
var faren = Math.round((temp * 9 / 5) + 32);
if(ctof == true){
$("#temp").html(faren);
}
else{
$("#temp").html(cel);
}
})
});
我什至包含了我的 html 代码,仅供参考
<!DOCTYPE html>
<html>
<head><title>Web App</title></head>
<body>
<div class="container">
<div class="row">
<header class="col-xs-12 text-center">
<h1>Free Code Camp </h1>
<h1>Weather App</h1>
</header>
<div class="col-xs-8 col-xs-offset-2">
<div class="text-center status">
<p><span id="city"></span> <span id="country"></span></p>
<p><span id="temp"><button id="#c/f">C/F</button></span></p>
<p id="desc"></p>
</div>
<div class ="info">
<div class="develop">Developed by = Shumaila Bi Shaikh</div>
<div class="langs">Created by: HTML,CSS,ANGULARJS</div>
</div>
<script src="Weather.js"></script>
【问题讨论】:
-
这在小提琴中有效。请参阅jsfiddle.net/bokav0z8/1 问题可能出在您的 HTML 结构上。
标签: javascript json ajax