【发布时间】:2019-11-14 22:06:44
【问题描述】:
我正在尝试将 opendata 从网站获取到我自己的网站。我正在使用此代码(从 w3schools 示例中获取)并尝试将其与我在 Excel/VBA 中的工作代码结合起来。 但不知何故,我无法让它在我的网站上运行......我的 html/javascript 中缺少什么?
HTML:https://codepen.io/1234cb/pen/ZEEqxJK
<body>
<h1>XMLHttpRequest</h1>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState > 1) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "https://www.energielabel.nl/api/v1/checkenergylabel", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
xhttp.setRequestHeader("Connection", "Keep-Alive");
xhttp.send("Postcode=2651ES&HouseNr=109&HouseNrAddition=");
document.getElementById("demo").innerHTML = this.responseText;
}
</script>
</body>
</html>
VBA:
Sub getopendata()
Set ObjHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://www.energielabel.nl/api/v1/checkenergylabel"
ObjHTTP.Open "POST", URL, False
ObjHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
ObjHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ObjHTTP.setRequestHeader "Connection", "Keep-Alive"
ObjHTTP.send ("Postcode=2651es&HouseNr=109")
ActiveCell = ObjHTTP.responsetext
Set ObjHTTP = Nothing
Set xmlDoc = Nothing
End Sub
【问题讨论】:
标签: javascript html ajax vba request