在HTML中显示API响应

时间:2016-10-10 06:32:39

标签: xml parsing weather-api openweathermap

我正在学习API并且是该过程的初学者,任何帮助都是有价值的。

我已经设法将数据im作为XML响应进行查找,并在chrome中记录了console.log。我不确定如何实际访问我在控制台中看到的#document,请参阅下面的代码和控制台打印输出



<!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>
&#13;
&#13;
&#13;

控制台显示:

&#13;
&#13;
200
OK
#document
  all of the XML is contained in here that I need to access and display
&#13;
&#13;
&#13;

我搜索了整个周末,但找不到我能理解的解决方案。

提前致谢

1 个答案:

答案 0 :(得分:-1)

我设法通过使用PHP和JSON作为替代方法来解决这个问题 - 在下面发布以帮助其他人处于同样的困境

&#13;
&#13;
float:left
&#13;
&#13;
&#13;