I am mostly a self-taught programmer and recently I've been messing around with the League of Legends API which uses JSON which I haven't used before.
This is part of my code which retrieves a player's ID:
function RiotAPI(phrase) {
...
this.request = function() {
return $.ajax({
url: url,
type: 'GET',
dataType: 'json',
data: {
},
});
}
}
function getID(key, name) {
phrase = getPhrase(2, key, 0, name);
api = new RiotAPI(phrase);
api.request().done(function(data)
{
window.console&&console.log(data[name]['id']);
document.getElementById("sID").innerHTML = data[name]['id'];
});
}
In this case the console properly logs the data in data[name]['id']. However if I do something like
id = data[name]['id']
then the variable 'id' is undefined. I understand that this is due to the nature of json, but what is a common way to deal with that?
Right now I have
document.getElementById("sID").innerHTML = data[name]['id'];
which surprisingly does work, and it functions like a very sad variable. Clearly there has to be a better way to handle this, right?
Thanks