help with parsing JSON in javascript

  • 1
  • Question
  • Updated 3 years ago
  • (Edited)
I have very little experience with coding and web development and this is way outside my experience but I'm trying to do this as a learning exercise. So please be patient with me :)

I have an html file I've written in notepad++ which includes a clock and will eventually include weather from weather underground. My javascript is contained in a separate file, and I'm successfully calling XMLHttpRequest(), open(), and send() with no errors. For example:

var weather = new XMLHttpRequest();"GET", "";, true);

I then call JSON.parse(weather.responseText). That's where I get the error: anonymous function.

But it works fine when I call JSON.parse from the chrome browser console!

I've been googling for two days trying to find an explanation. I've tried eval(), jQuery.parseJSON().
Nothing works in my html javascript but it works fine from the console. Can anyone tell me what I might be doing wrong?

I just found that if I commit the terribly evil crime of changing my from true to false then my code suddenly works!

I know that's not the proper way of doing things. do I need to include a weather.onreadystatechange function?
Photo of AidenDakari


  • 2 Posts
  • 0 Reply Likes

Posted 3 years ago

  • 1
Photo of afelicioni


  • 227 Posts
  • 42 Reply Likes
Hi, tried to use your code in a basic example in plain javascript.
Using the following
<!DOCTYPE html>
<meta charset="utf-8">
<title>hello weather</title>
<script type="text/javascript">
var weather = new XMLHttpRequest();
weather.onreadystatechange=function() {
  if (weather.readyState==4 && weather.status==200) {
    var data = JSON.parse(weather.responseText); 
}"GET", ""+WU_API_KEY+"/conditions/q/CA/San_Francisco.json", true);
<h1>hello world weather</h1>
weather in <span id="current_city">(loading)</span> is 
<span id="current_weather">(loading)</span>
testable after adding an api key on jsfiddle at I wasn't able to reproduce the anonymous function error.
Later, you mentioned jQuery but maybbe isn't clear if you plan to use it or not; so at the end seems you handled it and issues gone, or still looking for extra hints?
Photo of AidenDakari


  • 2 Posts
  • 0 Reply Likes
Thank you so much for the reply. I think I've got a handle on it now. My 'anonymous function' error was caused by trying to parse data that I had not yet received. I've now learned how to use .onreadystatechange and everything is working as it should now..

Thank you again! Was not using jQuery for this just because I haven't learned it yet. That part is next!
Photo of afelicioni


  • 227 Posts
  • 42 Reply Likes
That's fine you're welcome.
I endorse your determination to learn more about jQuery, it's a really useful library for sure, the slogan "write less, do more" really matches to reality.
Photo of Karin Meersman

Karin Meersman

  • 1 Post
  • 0 Reply Likes
This question and reply have been very helpful. I'm a beginner too and am probably doing the same exercise for freecodecamp.