我在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在Array中的JSON中返回了我的记录,如下所示:
sport_id:
[{"id":110,"sport_id":1,"league_id":32}]
league_id:
[{"id":110,"sport_id":1,"league_id":32}]
我无法获得sport_id
和league_id
的数量。我尝试添加此主题enter link description here中的代码:
if(sport_id[i]._type[0]=='sport_id'){
currentObj = pushNew(sport_id[i].nodeValue);
}
但我已经回复了错误:
statusText:OK状态:200 readyState:3
答案 0 :(得分:0)
将此代码用于所需的输出
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] + " ";
}