This is a long post. The TL/DR version: I sort of understand callbacks. And I’ve made successful AJAX calls in the past. I’m just strangely stumped by, um, exactly what code do I put where to use the results effectively?
I feel bad asking for the level of hand-holding that I’m about to, but I’ve been stalled on the Weather App for SIX WEEKS!!! The issue is my basic understanding of callbacks, and how to write an ajax (or just $.getJSON
) request in such a way that its response is usable outside the call.
Lemme break it down to the very beginning.
As I see it, there are three steps here:
- Find user’s location (ultimately, I’d prefer to go with the https and geolocation solution instead of IP). Presumably, set some kind of
var location
or the like to the response. - Use that location value in a call to a weather API.
- Use the weather conditions returned in a call to some graphics library API (or perhaps just hotlink to the limited number of possibilities).
After initial trouble with geolocation
, and turning to IP (I see now that I can simply slap https
into the URL bar to overcome that, and will probably go back to it) I was at this point: http://codepen.io/AbdiViklas/pen/BKEaYz?editors=1010
Obviously, though I can get a result from the IP location call, and put it into the DOM within that call, I can’t use it outside the call—like, say, in my next call to a weather API. I suppose I could nest the weather call inside the location call, but that idea… scares me. Where would it end?
Clearly, the problem is one of scope, and the solution seems to be callbacks. (Yes, I know that there are newer solutions like promises and let
, and I very much want to learn about them, but I wanted to know about the “old way” first, to appreciate just what the “new ways” are trying to improve upon.) I read about half a dozen pages about callbacks, and just yesterday I proved to myself that I can actually use callbacks to make something happen: https://codepen.io/AbdiViklas/pen/YWWLaP?editors=1011
I’m still having trouble, though, figuring out how to work that functionality into an AJAX call. Where do I, um, put the callback function? and the function that calls it? Is it like this? http://codepen.io/AbdiViklas/pen/RRRyZN?editors=1010
… clearly not; something’s very wrong there. I’ve gotten things to work before using $.getJSON
instead of $.ajax
; I know in that context you just “put the callback after the URL”—like this?
http://codepen.io/AbdiViklas/pen/XKKPxy?editors=1010
… sigh, I guess not like that.
This seems like such a basic issue—make calls to more than one API, use the responses usefully in other calls—that I would have thought I could figure it out on my own. But dude, SIX WEEKS. I can feel myself seriously losing traction and momentum, and I want to get a move on.