1

I have this basic "GET" request on a demo page I'm building via localhost. Both json and html file are in the same folder.

http://localhost.com/test.html http://localhost.com/myList.json 

Unfortunately when making the request I get this error:

Uncaught ReferenceError: results is not defined. Uncaught TypeError: Cannot set property 'innerHTML' of null 

My html is as follows.

 <html> <head> <title>Test</title> <script type="text/javascript"> function get_json_request() { var httpRequest; httpRequest = new XMLHttpRequest(); httpRequest.open("GET", "myList.json", true); httpRequest.setRequestHeader("Content-type", "application/json", true); httpRequest.onreadystatechange = function() { if (httpRequest.readyState === 4 && httpRequest.status === 200) { var data = JSON.parse(httpRequest.responseText); var results = document.getElementById("results"); results.innerHTML = data.user; } else { alert('There was a problem with the request.'); } } httpRequest.send(null); results.innerHTML = "Processing...."; } </script> </head> <body> <div id"results"></div> <script type="text/javascript">get_json_request();</script> </body> </html> 

Any help appreciated.

2
  • 1
    Typo <div id"results"> Commented Jul 1, 2013 at 21:19
  • possible duplicate of How to return AJAX response Text? Commented Jul 1, 2013 at 21:23

1 Answer 1

1

You're setting the reference to "results" in a conditional statement. If that variable is not initialised, it will naturally return null.

Sign up to request clarification or add additional context in comments.

2 Comments

That's a potential second problem. The first problem is that Ajax is asynchronous.
See the earlier vote to close as duplicate with associated comment on the question itself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.