我有一个API,它应该返回一些文本JSon字符串。
http://api.census.gov/data/2010/sf1?get=P0010001&for=county:013&in=state:08
我想使用JavaScript查询这个API并在HTML中显示。代码如下所示:
//html
<input type="submit" value="Get City" onclick=" getpop()">
//JS:
function getpop() {
var nereq2 = new XMLHttpRequest();
nereq2.open("GET", "http://api.census.gov/data/2010/sf1?get=P0010001&for=county:013&in=state:08", true);
nereq2.onreadystatechange = function () {
if (nereq2.readyState == 4) {
var temp3 = nereq.response; **//problem start at here, which always return empty*******
document.getElementById("fs").innerHTML = temp3;
};
};
nereq2.send();
}当我单击链接时,它正确地返回JSon,但是当我使用代码进行查询时,它返回为空。我不知道它是否与浏览器设置有关,还是还有其他问题?
发布于 2016-05-20 03:27:07
你有个打字错误。nereq.response应该是nereq2.response。
工作JSFiddle -(这里使用https,因为JSFiddle需要)
https://stackoverflow.com/questions/37337296
复制相似问题