I am working with an API and mananged to get the .ajax function working just fine. Now, I need to use the information from the API outside of the .ajax function. To do that, I am trying to use jQuery's .done function, but it's not working. I looked at a few solutions to similar problems and I have had no luck. I tried it with and without the $.when function, but neither works.
//create user prototype
function user(name,logo,status,streamLink,streamViews,streamGame){
this.name = name;
this.logo = logo;
this.status = status;
this.streamLink = streamLink;
this.streamViews = streamViews;
this.streamGame = streamGame;
this.getUserInfo = function(userName) {
return $.ajax({
dataType: "jsonp",
data:{},
url:'https://api.twitch.tv/kraken/users/'+userName+'?callback=?',
success:function(data){
this.name = data.display_name;
this.logo = data.logo;
this.bio = data.bio;
$('.test').html(this.logo);
}
})
}
}
//create user for each stream
var freeCodeCamp = new user('freeCodeCamp');
var medrybw = new user('medrybw');
//call getUserInfo function
freeCodeCamp.getUserInfo('freeCodeCamp');
medrybw.getUserInfo('medrybw');
//display medrybw status (test) - not working!
when(medrybw.getUserInfo()).done(function(data){
alert('test');
$('.test2').html(medrybw.bio);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test"></div>
<br>
<br>
<div class="test2"></div>
The part that is not working is that last function. If I remove the $.when part, I get the "test" alert working fine, but not the other line. With the $.when, neither line runs. I am really confused.