Monday, April 3, 2017

Json in array Javascript and Ajax

I write code in JS (Ajax):

function getSportIdAndLeagueId() {

    var action = 'http://localhost:8012/football/football/public/admin/ajax/';
    var teamId = document.getElementById('team_a_id').value;

    var question = "";
    question = new XMLHttpRequest();

    question.onreadystatechange = function() {

        if ( question.readyState == 4 && question.status == 200 ) {
                // document.getElementById('sport_id').innerHTML = question.responseText;
                var allRecord = question.responseText;
                var sport_id = "";
                var league_id = "";

                for (var i = 0; i < allRecord.length; i++) {
                    sport_id = sport_id + allRecord[i];
                    league_id = league_id + allRecord[i];        
                }

                document.getElementById('sport_id').innerHTML = sport_id;
                document.getElementById('league_id').innerHTML = league_id;
        } else {

            document.getElementById('sport_id').innerHTML = 'Error: ' + question.statusText + ' code: ' + question.status + ' readyState: ' + question.readyState;

        }

    }

    question.open("get", action + teamId, true);
    question.send();

}

Ajax returned me records in JSON in Array, look like this:

sport_id:
[{"id":110,"sport_id":1,"league_id":32}]
league_id:
[{"id":110,"sport_id":1,"league_id":32}]

I can't get only number of sport_id and league_id. I trying add code from this theme enter link description here:

if(sport_id[i]._type[0]=='sport_id'){
    currentObj = pushNew(sport_id[i].nodeValue);
}

But I have returned Error:

statusText: OK Status: 200 readyState: 3



via michal

Advertisement