How do I store data from an ajax call?

I can successfully access the data:

now I want to store it in a variable called “streams”


while I’m at it… then how do you access javascript functions and variables written in one document in another html document? ie:

I’m not new to html and css, but then I started learning JS and integrating API’s and now I’m completely lost.

Since you’re using jQuery, you can easily access the HTML DOM.

Consider an iframe element in your HTML:

<iframe src="" width="1920" height="1110" style="border: none;"></iframe>

You can interact easily with jQuery:

$('iframe').attr('src', 'http://www.twitch.tv/' + stream.channel.name + '/embed');

Information on jQuery selectors: https://api.jquery.com/category/selectors/

Essentially my goal is to have the ajax call attached to a button
which I’ve successfully done, but now I need to store the data when the button is clicked as well.
I understand that ajax is asynchronous and I was wondering if there was a way around that?

If you’re trying to set a variable to contain the data from the call, then you’d set it after receiving data from the call. The jQuery ajax call is barebones, and you can instead us their JSON call to simplify your request: http://api.jquery.com/jQuery.getJSON/ You should refer to the jQuery documentation for how to handle responses from requests. They provide examples in their documentation.

You seem new to JS, so you should try reading a guide and following tutorials before attempting to create anything with it: https://developer.mozilla.org/en-US/docs/Web/JavaScript JavaScript is largely an asynchronous language that depends on callback functions to handle data passing from asynchronous event completion.

Yeah I am extremely new to JS, thanks for the links!
I thought that starting a project would force me to learn as I go,
but I guess I got a little in over my head.

It looks like you are trying to return the data from your ajax call to the function which will not happen due to the asynchronous nature of the ajax call. I am gueesing that this function is inside of your button click handler. so instead, drop the var streams; declaration and do this…

//button click handler
$("#myButton").on('click', function() {
apiCall(function(streams) {
//do things with the stream object
}
});
//ajax call function
function apiCall(callback) {
//contents of your ajax call goes here
//and instead of the streams = data; line in your success function do callback(data);
}

hope this helps

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.