2016-10-10 6 views
0

私はAPIを学習しており、プロセスの初心者です。どんな助けも貴重です。HTMLでAPIレスポンスを表示

私はXMLレスポンスとして探しているデータを取得して、それをconsole.loggedにクロムで記録しました。

200 
 
OK 
 
#document 
 
    all of the XML is contained in here that I need to access and display
:私は実際に

<!doctype html> 
 
<html> 
 
<head> 
 

 
<script> 
 
window.onload = function() { 
 
var xhr = new XMLHttpRequest(); 
 
xhr.open("GET","http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&mode=xml&APPID=92cc96ecfe265f251d814b66592a7848",false); 
 
xhr.send(); 
 
console.log(xhr.status); 
 
console.log(xhr.statusText); 
 
console.log(xhr.responseXML); 
 
var response = xhr.responseXML; 
 
var xmlString = (new XMLSerializer()).serializeToString(response); 
 
if(xhr.status == 200){ 
 
document.getElementById("document").innerHTML = xmlString; 
 
} 
 
} 
 
</script> 
 
<meta charset="UTF-8"> 
 
<title>Weather API Test</title> 
 
</head> 
 

 
<body id="body"> 
 
<div id="document"></div> 
 
</body> 
 
</html>
とコンソールディスプレイコードとコンソール印刷下記参照私は、コンソールに表示この#documentへのアクセス方法がわかりませんよ

私は週末全体を検索しましたが、私が理解できる解決策です。

答えて

-1

事前に

おかげで、私は代替としてPHPとJSONを使って、それを把握するために管理 - 同じジレンマ

<!doctype html> 
 
<html> 
 
<head> 
 

 

 

 
<meta charset="UTF-8"> 
 
<title>API TEST JSON</title> 
 
</head> 
 

 
<body> 
 
<?php 
 
error_reporting(E_ALL); 
 
ini_set('display_errors', 1); 
 
\t $place = "capetown,za"; 
 
\t $json_string = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=$place&units=metric&APPID=API-KEY-GOES-HERE"); 
 
\t $parsed_json = json_decode($json_string); 
 
\t $country = $parsed_json->sys->country; 
 
\t $wind = $parsed_json->wind->speed; 
 
\t $city = $parsed_json->name; 
 
\t $temp = $parsed_json->main->temp; 
 
\t $clouds = $parsed_json->weather[0]->description; 
 
\t if($clouds == "clear sky"){ 
 
\t \t echo ("<img src='http://placehold.it/200x200'>"); 
 
\t } 
 
?> 
 

 
<table border="1px solid black"> 
 
<tr> 
 
<td style="color:red;">Country</td> 
 
<td style="color:red;"><?php echo $country;?></td> 
 
</tr> 
 
<tr> 
 
<td style="color:red;">City</td> 
 
<td style="color:red;"><?php echo $city;?></td> 
 
</tr> 
 
<tr> 
 
<td style="color:red;">Temperature</td> 
 
<td style="color:red;"><?php echo $temp;?></td> 
 
</tr> 
 
<tr> 
 
<td style="color:red;">Wind Speed M/S</td> 
 
<td style="color:red;"><?php echo $wind;?></td> 
 
</tr> 
 
<tr> 
 
<td style="color:red;">Cloudiness</td> 
 
<td style="color:red;"><?php echo $clouds;?></td> 
 
</tr> 
 
</table> 
 
</body> 
 
</html>

+0

で他人を助けるために、以下の投稿はなぜダウン投票? – anthonytherockjohnson

関連する問題