0
我正在使用openweathermap API製作API項目,而我的問題是我無法獲取名稱和溫度來更改頁面的html。 JSON的名稱和臨時名稱應該如何定位,並將其放在頁面上以取代.city
和.temperature
?使用它是鍵入一個位置並返回名稱和溫度。謝謝。基於JSON數據的Html
$(document).ready(function(){
//var locname = $('.city');
var temp = $('.temperature');
var APIKEY = '5d2601f4620847da11e653d59d14ae18';
var loc = $('#search').val();
//var responsetext = $(url).html();
function updateByCity(loc){
\t var url = "http://api.openweathermap.org/data/2.5/weather?q=" + loc + "&APPID=" + APIKEY;
\t sendRequest(url);
}
/*function sendRequest(url){
\t var url = "http://api.openweathermap.org/data/2.5/weather?q=" + loc + "&APPID=" + APIKEY;
\t var xmlhttp = new XMLHttpRequest();
\t xmlhttp.onreadystatechange = function(){
\t \t var url = "http://api.openweathermap.org/data/2.5/weather?q=" + loc + "&APPID=" + APIKEY;
\t \t if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
\t \t var data = JSON.parse(xmlhttp.responseText);
\t \t locname = data.name;
\t \t temp = data.main.temp;
\t \t }
\t }
\t xmlhttp.open("GET", url, true);
\t xmlhttp.send();
}*/
$('.enter').click(function(event){
event.preventDefault();
var temp = $('.temperature');
var loc = $('#search').val();
var url = "http://api.openweathermap.org/data/2.5/weather?q=" + loc + "&APPID=" + APIKEY;
\t //$('.temperature').html(temp);
\t //$('.city').html(locname);
\t console.log(url);
\t var xmlhttp = new XMLHttpRequest();
\t xmlhttp.onreadystatechange = function(){
\t \t var url = "http://api.openweathermap.org/data/2.5/weather?q=" + loc + "&APPID=" + APIKEY;
\t \t console.log("lol");
\t var data = $.getJSON(url)
\t var datatext = data.responseText;
\t \t var locname = data.name;
\t \t console.log(datatext);
\t \t console.log(url);
\t \t //$(temp).text(data.main.temp);
\t $('.city').text(locname);
\t };
\t xmlhttp.open("GET", url, true);
\t xmlhttp.send();
});
});
html{
background-color:dodgerblue;
margin:0px;
padding:0px;
width:100%;
}
body{
margin:0px;
padding:0px;
}
.container{
width:900px;
height:700px;
background-color:white;
position:absolute;
left:180px;
-webkit-box-shadow:0 0 10px rgba(0, 0, 0, 0.5);
\t -moz-box-shadow:0 0 10px rgba(0, 0, 0, 0.5);
\t box-shadow:0 0 10px rgba(0, 0, 0, 0.5);
}
.citysearch{
left:375px;
top:40%;
margin:0px;
padding:0px;
position: absolute;
border:solid black;
}
.results{
\t width:300px;
\t height:200px;
\t position:absolute;
\t left:300px;
\t color:white;
\t top:50%;
\t background-color:#008B8B;
}
.results:hover{
-webkit-box-shadow:0 0 10px rgba(0, 0, 0, 0.5);
\t -moz-box-shadow:0 0 10px rgba(0, 0, 0, 0.5);
\t box-shadow:0 0 10px rgba(0, 0, 0, 0.5);
}
.inputname{
\t font-family:Lato;
\t position:absolute;
\t top:30%;
\t left:350px;
}
.enter{
width:100px;
height:50px;
position:absolute;
top:265px;
left:250px;
background-color:#00FA9A;
border:#00FA9A;
}
.icons8{
position:relative;
top:650px;
}
.icon{
\t position:absolute;
\t left:400px;
}
.temperature{
\t font-family:Lato;
\t margin:5px;
\t font-size:40px;
\t position:absolute;
\t right:110px;
}
.city{
font-family:Lato;
margin:5px;
position:absolute;
right:130px;
top:55px;
}
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" type="text/css" href="weather.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src ="weather.js"></script>
</head>
<div class="container">
<body>
<!-- Partly Cloudy Day icon by Icons8 -->
<img class="icon icons8-Partly-Cloudy-Day" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAHdklEQVR4Xu2c329URRTHv7O7t9AAtRUrFhNhtyghYNgmakKIQNVueBKJfwDwpg/+wCcfEOHJ+KDCE8SQUP4AY3nSFIktAcVYYxttiKRsGx6oSdUuULCwP46Z3VaW7e7OuT9nLzv39c6cM+f7mTkzd2Z2BcyjVQGh1btxDgN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v6mkbQHE+KF1L6ZdHlM04F1TGTITjViR2hJt2yge9IwCLBkqro8IRIrGuWTblynu+3O+3hCWFd/zfEq4APAAhJ+j8uwT0CVFYDYjWEKf2ak2fIxKZiKArRPhBRMRoFNEhJz2+0rgnAB4xsQMNxwAIVO6lzgwAA0CzAprdmxFgAGhWQLN7MwIMAM0KaHZvRoABoFkBze7NCDAANCug2b0ZAQaAZgU0u/8PU/Ssf10cbiEAAAAASUVORK5CYII=" width="96" height="96">
<p class="inputname">Look up weather for somewhere.</p>
<input type="submit" class="enter" value="Enter"><input type="text" name="Button" class="citysearch" id="search">
<div class="results">
<div class="temperature">N/A</div>
<div class="city">N/A</div>
</div>
<a href="https://icons8.com" class="icons8">Icon pack by Icons8</a>
</body>
</div>
</html>
嘗試更換'TEMP = data.main.temp;'和'temp.text(data.main.temp);'這是否幫助任何? –
它是否成功回調?嘗試使用'$ .get()'而不是'$ .getJSON()' – Developer
好了,現在它會正確記錄除responseText以外的所有內容。我不小心複製並粘貼了一個不是最新的版本到這裏。事情是響應文本回來undefined。所以它沒有正確抓取JSON。有任何想法嗎? – William