I have an issue when trying to get some data from a json.
Usually I have to hardcode the JSON variable I want (ID), such as
success: function (json) {
alert(json.ID.name);
}
Which works just fine, however I need to make this dynamic as my ID needs to update per request.
When I try the following
success: function (json) {
// ID is given above and it is working fine
var userID = ID.replace(" ", "");
userID = userID.toLowerCase().trim();
alert(json.userID.name);
}
Doing that gives me an error
Uncaught TypeError: Cannot read property 'name' of undefined
The userID when alerted gives the same String as ID, but sticking it in the json does not seem to work. Any reason why?
Thanks!
Example (unfortunately I cannot post my API key)
var ID = "";
ID = $("#theID").val();
if (ID != "") {
$.ajax({
url: 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' + ID + '?api_key=<KEY>',
type: 'GET',
dataType: 'json',
data: {
//email: $('#sube').val()
},
success: function (json) {
var userID = ID.replace(" ", "");
userID = userID.toLowerCase().trim();
document.getElementById("who").innerHTML = "Who: " + json.userID.name;
document.getElementById("level").innerHTML = "Level:" + json.userID.id;
document.getElementById("id").innerHTML = "ID: " + json.userID.summonerLevel;
document.getElementById("idCall").innerHTML = "Status: SUCCESS";
document.getElementById("url").innerHTML = "URL: " + 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' + userID + '?api_key=<KEY>';
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
document.getElementById("idCall").innerHTML = "Status: FAILED";
}
});
} else {
document.getElementById("idCall").innerHTML = "Status: FAILED";
}