我在控制台里什么都没有。它应该改变包含html到openweatherapp从这个邮政编码获取的信息(我检查这个zip并且它存在并且函数updateByZip创建了良好的链接)。第一个代码是JS和第二个HTML。 JS
var temp;
var loc;
var humidity;
var icon;
function bg() {
var backs = ["http://wallpapercave.com/wp/JthAGYd.jpg", "http://www.desktopwallpaperhd.net/wallpapers/20/b/welshdragon-landscapes-cometh-background-204971.jpg", "http://s1.picswalls.com/wallpapers/2014/08/08/scottish-landscape-desktop-backgrounds_015751372_152.jpg", "http://img.wallpaperfolder.com/f/4313075B95B2/amazing-winter-backgrounds-6770538-landscape.jpg"];
var ran = Math.floor(Math.random() * (backs.length));
$('body').css("background-image", "url('" + backs[ran] + "')")
//document.body.style.background = "url/'('" + backs[0] + "') no-repeat";
}
function tempCF() {
var x = document.getElementById("temp").innerHTML;
var y = document.getElementById("CF").innerHTML;
x = parseInt(x);
if (y == "C") {
x = Math.floor((9 / 5 * x + 32));
document.getElementById("temp").innerHTML = x;
document.getElementById("CF").innerHTML = "F"
} else if (y == "F") {
x = Math.floor((x - 32) * 5 / 9);
document.getElementById("temp").innerHTML = x;
document.getElementById("CF").innerHTML = "C";
};
}
function updateByZip(zip) {
var APPID = "55e568aa04114cdf3dc4b90c9ae0a60c";
var url = "api.openweathermap.org/data/2.5/weather?zip=" + zip + "&APPID=" + APPID;
sendRequest(url);
}
function sendRequest(url) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
var weather = {};
weather.temp = data.main.temp;
weather.humidity = data.main.pressure;
weather.loc = data.name;
//weather.icon = data.weather[0].id;
update(weather);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function update(weather) {
document.getElementById("temp").innerHTML = weather.temp;
document.getElementById("humidity").innerHTML = weather.humidity;
document.getElementById("loc").innerHTML = weather.loc;
//document.getElementById("icon").innerHTML = weather.icon;
};
window.onload = function() {
var temp = document.getElementById("temp").innerHTML;
var loc = document.getElementById("loc").innerHTML;
var humidity = document.getElementById("humidity").innerHTML;
//var icon = document.getElementById("icon").innerHTML;
updateByZip(94040);
//weather.icon = "https://cdn1.iconfinder.com/data/icons/hawcons/32/700175-icon-1-cloud-128.png";
};
HTML
<head>
<link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
</head>
<body onclick="bg()">
<div class="container-fluid">
<div class="wrapper">
<div class="row">
<div class="title"><span id="loc"> Your location</span></div>
</div>
<div class="row">
<div class="col-ms-4">
<div class="fircol"><span id="temp">0</span>°<span onclick="tempCF()" id="CF">C</span></div>
</div>
<div class="col-ms-4">
<div class="seccol"><span id="humidity">Rain</span></div>
</div>
<div class="col-ms-4">
<div class="thicol"><span class="icon"><img src= https://cdn1.iconfinder.com/data/icons/hawcons/32/700175-icon-1-cloud-128.png></img></span></div>
</div>
</div>
</div>
</div>
</body>
答案 0 :(得分:2)
您的API网址不正确。 你应该写:
var url = "http://api.openweathermap.org/data/2.5/weather?zip=" + zip + "&APPID=" + APPID;