When I first started working on the Wikipedia Viewer page, all of this API stuff sounded like complete gibberish. I tried searching in Google, all over these forums, and even copied some live working examples from working sites made by other people. Nothing worked, so eventually I decided to cut my losses and move onto the next project, rather than risk getting fed up and having the whole thing fizzle out due to lack of progress.
Well, I’ve managed to finish all of the advanced projects and algorithms, and I’m now back to this same thing. Despite everything I’ve learned from the rest of this curriculum, this API stuff still might as well be written in Swahili, or Martian, or Braille. I’ve copied more examples, Googled everything I can think of (which led me to more stuff to copy), and read through at least a dozen pages of ancient Gaelic nonsense on the API documentation page. This is ridiculous.
In my code, I have copied “working” examples of both an AJAX call and a getJSON call, neither of which I fully understand, and neither of which actually produce anything. I have no idea how to debug this or what kinds of things to look for or how to even ask the right questions to describe wtf is going on here. What is going on, and why is this so difficult compared to all of the other “advanced” stuff that I was able to plow through in a matter of a few days?
I apologize for the rant, but this is just not working for me and there’s simply no reason it should be this hard.
Asynchronicity is one of the most difficult concepts in programming, and AJAX is no exception. Not only does it seem like there are a bunch of different functions that do the same thing, and the whole process doesn’t quite fit in with the straightforward, procedural manner we’re taught to code in the beginning. The Wikipedia Viewer project complicates things a bit more since the WikiMedia API is absurd and arcane. I’d suggest taking a step back, learn to make simpler AJAX calls, then get your head around a WikiMedia call. I have a link to the settings I use here. To learn how to make an AJAX call, YouTube is a great friend. Here is a video I really like, but if you just search “AJAX”, you’ll find many more. I made a quick example of an AJAX call to Github, which should make more sense after you watch the linked video.
@owel I had both types of API call in the code because I was trying to get either one of them to work. I had used one in the Random Quote project, and the other in the Weather project, and I was basically just trying to get something to work.
EDIT: The Richard Feynman part was part of the example code I copied from the documentation page.
@marzelin I guess long-term debugging guidance would probably be more beneficial.
Here’s my process. First, I try out the API URL on it’s own… type it on your browser and see if you’re getting the response you think you’re getting.
Also helps to click on Dev tools, and the Network tab and see if the calls to the API server are successful, and if you got any response (or the correct response). Also check Console if there are any error messages brought up.
Then finally, analyze the JSON response and try to see how you can extract the different parts that you need. Use the console for this to play around. sometimes the JSON response can be convoluted… I particularly didn’t like Wikipedia’s output.
Once you can confirm everything, then you write the code to display the JSON data back to html page.
Now that’s helpful. I sure hope I was drunk when I read through the curriculum and just missed the part where this was explained somewhere, because this was really helpful to know.
I was able to get the raw url to work for the $.getJSON call, and it seemed to show valid response data. However, when I went to the Network tab of dev tools (didn’t know that tab existed either), the Preview section says “Failed to load response data”. I’m not quite sure what that means, but it’s more info than I had before.
When debugging, you want to take a piece of functionality and test it in isolation.
First, you should check if the data is fetched correctly:
It seems that everything is working it this piece of code, so you test another piece - the form submission:
As you can see, the code in the submit handler is executed but the site reloads immediately after that. That’s your bug.
You ask google about this behavior and learn that you should return false from submit handler.
…and with all of the page refreshes that naturally happen when I’m working in CodePen, plus the fact that my work laptop has frequent CPU spikes that force unscheduled Pomodoro-style breaks in the action, I never would have seen that. Turns out the API call was actually working all along. Thanks!