user3491987 user3491987 - 1 year ago 113
JSON Question

JavaScript Query Json String from (

I have an API which should return some text JSon String.

I wan to use JavaScript to query this API and display in the HTML element. The code looks like this:

<input type="submit" value="Get City" onclick=" getpop()">
function getpop() {
var nereq2 = new XMLHttpRequest();"GET", "", 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;

When I click the link it returns the JSon properly, however when I use the code to query, it returns empty. I don't know whether it related to the browser setup or there are some other issues?

Answer Source

You have a typo. nereq.response should be nereq2.response.

Working JSFiddle - (using https here because JSFiddle requires that)