JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Sean Lamoreaux
Sean Lamoreaux
13,468 Points

I'm very confused with this project! Please help?

I'm very confused with this workspace.

1) I understand everything in the video, but my "widget.js" code is not creating the widget at all even though I've typed it out exactly as Dave did in the video (from what my eyes can see). I can't see any errors, and the console doesn't show any errors either.

2) The workspace has a folder called "finished" that presumably shows what the finished files should look like... but the widget.js file under "finished" is DIFFERENT from what Dave typed in the video! Why is it like that??

3) If I simply copy and paste the widget.js code from "finished" into the widget.js file I was working on, then and ONLY then does the widget actually show up on the page! But again, this code was not what Dave demonstrated in the video.

So which is supposed to be the correct code-- The one in the video or the one in the "finished" portion of the workspace? Because the one that worked for me was the latter. I can't even begin to wrap my brain around what is going on with this thing. Am I insane? Did I miss something?

Here is the link to the snapshot of my workspace for this video:

2 Answers

Billy Bellchambers
Billy Bellchambers
21,689 Points

Hi Sean,

Having had a look over your code I can see there is a typo in the following :

1 - Your code reads: if (xhr.readystate === 4) { ----note the missing captial on the state, this is causing your function to never fire as the parameters are never met.

and should be

if (xhr.readyState === 4) {

this should resolve the issue there.

2 - The final document is just an example piece and one way of solving the problem this one includes an addition check for the xhr.status to see if there is any recorded errors, a code of 200 is OK.

3 - Note number 1

Hope this helps clear up your problem.

Sean Lamoreaux
Sean Lamoreaux
13,468 Points

Hey Billy,

Thank you! Of course it would be a silly typo on my part. How typical. Just shows you how helpful it can be to have another pair of eyes look at your code. I wasted so much time re-reading and trying to look for typos, that my eyes got too tired to find any. I'm surprised the console didn't show an error though. Anyway now I can move on without sweating it.