I have a form that is used to lookup an array and return results with AJAX and jQuery. I have one function working, but another isn't, and I'm sure at this point I'm confusing myself with over analysis and long hours of troubleshooting. Please let me know what I'm missing.
the basic form elements:
<form name="theForm">
<input id="userName" value="myName" />
<input id="sumID" value="9999999" />
<input type="submit" onClick="summonerLookUp();" />
</form>
The basic array:
{"myName": {
"id": 9999999,
"name": "myName",
"profileIconId": 673,
"revisionDate": 1406601264000,
"summonerLevel": 26
}}
The working Javascript:
function summonerLookUp() {
var ID = "";
ID = $("#userName").val();
setKey();
if (ID !== "") {
$.ajax({
url: 'http://url-to-api',
type: 'GET',
dataType: 'json',
data: {
},
success: function (json) {
var userID = ID.replace(" ", "");
userID = userID.toLowerCase().trim();
summonerLevel = json[userID].summonerLevel;
summonerID = json[userID].id;
document.getElementById("sLevel").innerHTML
= summonerLevel;
document.getElementById("sID").innerHTML
= summonerID;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner data!");
}
});
} else {
}
}
Returns:
Summoner Level: 26
Summoner ID: 9999999
Now when I try to access the "Name" property within "pages" for this user ID in the following array:
{"9999999": {
"pages": [
{
"masteries": [
{
"id": 4313,
"rank": 2
},
{
"id": 4121,
"rank": 1
},
{
"id": 4134,
"rank": 3
}
],
"id": 41932694,
"name": "Offense Main",
"current": true
},
{
"masteries": [
{
"id": 4123,
"rank": 3
},
{
"id": 4311,
"rank": 1
},
],
"id": 41932698,
"name": "Utility",
"current": false
},
{
"masteries": [
{
"id": 4143,
"rank": 3
},
{
"id": 4162,
"rank": 1
},
{
"id": 4131,
"rank": 1
}
],
"id": 41932702,
"name": "Offense",
"current": false
},
],
"summonerId": 9999999
}}
using the following, I don't get what I'd expect (i.e.,Offense Main):
function masteriesLookUp() {
var ID = "";
ID = $("#sumID").val();
setKey();
if (ID !== "") {
$.ajax({
url: 'http://url-to-api',
type: 'GET',
dataType: 'json',
data: {
},
success: function (json) {
var userID = ID;
summonerMastery = json[userID].pages;
document.getElementById("sMastery").innerHTML
= summonerMastery;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner data!");
}
});
} else {
}
}
Instead this returns:
[object Object],[object Object],[object Object],[object Object]
I'm sure there are several issue, but I suspect the following line specifically:
summonerMastery = json[userID].pages;
json[userID].pages[0].nameto get "Utility" it would bejson[userID].pages[1].name, and so on.name(or any other) property from eachpageobject.