1

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

1 Answer 1

0

Use this code for your desired output

question.responseType = 'jsonp';

  for (x in sport_id) {
        document.getElementById("sport_id").innerHTML += sport_id[x] + " ";
    }

 for (x in league_id) {
        document.getElementById("league_id").innerHTML += league_id[x] + " ";
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Stil I have this same error: statusText: OK Status: 200 readyState: 3
I have updated code. And I think your data format is not standard, means JSONP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.