我一直在構建一個Web應用程序,昨天我嘗試向其中添加一些天氣數據。我構建了這個簡短的HTML文檔,它在瀏覽器中查看時非常完美。但是,今天我部署到Zeit和Firebase主機,但JavaScript未能執行並且什麼也沒有發生。正如你在這裏看到的(http://imgur.com/a/TFnEi),沒有任何錯誤。我該如何解決這個問題?HTML中的JavaScript可在本地使用,但不能在部署到服務器時使用
<!DOCTYPE html>
<html>
<head>
<title>Display Weather</title>
</head>
<body>
<style>
h1{
font-family: Arial;
margin:auto;
}
p{
font-family: Arial;
margin:auto;
}
img{
border: 2px solid blue;
border-radius: 10px;
}
</style>
<h1> Current weather in Berlin: </h1>
<p id="forecast_metric">Gathering data...</p>
<p id="temp_c">Gathering data...</p>
<p id="WIP">No data</p>
<p id="weathercond">Gathering data...</p>
<p id="visibility">Gathering data...</p>
<p id="updated">Gathering data...</p>
<img id="weathericon" src="test.png">
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', "http://api.wunderground.com/api/APIKEY/conditions/forecast/q/Germany/Berlin.json", true);
xhr.send();
xhr.onreadystatechange = processRequest;
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
var temp_c = response.current_observation.temp_c
var weathercond = response.current_observation.weather
var weathericon = response.current_observation.icon_url
var updated = response.current_observation.observation_time
var visibilitykm = response.current_observation.visibility_km
alert('The temperature is ' + temp_c + ' degrees Celsius in Berlin right now. The Forecast for ' + response.forecast.txt_forecast.forecastday[1].title + ' is : ' + response.forecast.txt_forecast.forecastday[1].fcttext_metric);
document.getElementById("forecast_metric").innerHTML = "The current forecast for today is: " + response.forecast.txt_forecast.forecastday[1].fcttext_metric
document.getElementById("temp_c").innerHTML = "Temperature: " + temp_c + " degrees Celsius"
document.getElementById("weathercond").innerHTML = "Weather: " + weathercond
document.getElementById("visibility").innerHTML = "Visibility: " + visibilitykm + " km"
document.getElementById("updated").innerHTML = updated
document.getElementById("weathericon").src = weathericon;
}
}
</script>
</body>
</html>
編輯: 我發現這個問題 - 在瀏覽器不接受HTTP和HTTPS要求,而不是。儘管感謝您的答案!
顯示在控制檯中的任何東西?你從API獲取了什麼東西,還是在那個時候掛? – Snowmonkey
究竟是在部署的應用程序中的端點返回? – Kejt
它工作正常 - 一切工作在我的Mac - –