Javascript语法错误“超出范围”

时间:2012-11-15 20:07:26

标签: javascript syntax

我一直在进行Web开发很长一段时间,并且从未见过使用JavaScript的这种行为。这是我开始使用的代码:

function processLogin() {
    if (loginReq.readyState == 4) {
        var data = eval('(' + loginReq.responseText + ')');
        data = data.userData;
        var focus = data.team.focus.coordinates;
        thisTeam = new Team(data.team.id, data.team.missionId, data.team.name, data.team.operatingArea.coordinates[0]);
        if (data.team.zoomLevel != '') {
            thisTeam.zoomLevel = data.team.zoomLevel;
        }
        if (focus.length > 0) {
            thisTeam.focusLat = focus[1];
            thisTeam.focusLon = focus[0];
        }
        for (var i = 0; i < data.teams.length; i++) {
            var temp_team = new Team(data.teams[i].id, data.teams[i].missionId, data.teams[i].name, []);
            teams.push(temp_team);
        }
        var teamDropDownText = [];
        for (var j = 0; j < teams.length; j++) {
            if (thisTeam.teamId == teams[j].teamId) {
                teamDropDownText.push('<option value="' + teams[j].teamId + '" selected="selected">' + teams[j].name + '</option>');
            } else {
                teamDropDownText.push('<option value="' + teams[j].teamId + '">' + teams[j].name + '</option>');
            }
        }
        $('#addIncidentTeam').html(teamDropDownText.join(''));
        $('#editIncidentTeam').html(teamDropDownText.join(''));
        // When all this has finished, make the
        //      rest of the calls to get the rest of the data
        startTimer();
        downloadDevices();
        initializeMap();
    }
}

我所写的内容并不重要,让我解释原因。

thisTeam.zoomLevel = data.team.zoomLevel;之后带有单个分号的行在firebug中给出了语法错误。我阅读并重新阅读了我的代码,但无法弄清楚我做错了什么,所以我把分号放在与thisTeam.zoomLevel = data.team.zoomLevel相同的行上,它告诉我它在空行上有语法错误!< / p>

要做另一个测试,我将整个函数移动到它自己的JavaScript文件中并将该行后的所有内容放在一行上,甚至尝试压缩上面的一些代码,所以现在看起来像这样:

function processLogin() {
if (loginReq.readyState == 4) {
    var data = eval('(' + loginReq.responseText + ')');
    data = data.userData;
    var focus = data.team.focus.coordinates;
    thisTeam = new Team(data.team.id, data.team.missionId, data.team.name, data.team.operatingArea.coordinates[0]); if (data.team.zoomLevel.length > 0) { thisTeam.zoomLevel = data.team.zoomLevel; } if (focus.length > 0) { thisTeam.focusLat = focus[1];thisTeam.focusLon = focus[0];} for (var i = 0; i < data.teams.length; i++) { var temp_team = new Team(data.teams[i].id, data.teams[i].missionId, data.teams[i].name, []); teams.push(temp_team); } var teamDropDownText = []; for (var j = 0; j < teams.length; j++) { if (thisTeam.teamId == teams[j].teamId) { teamDropDownText.push('<option value="' + teams[j].teamId + '" selected="selected">' + teams[j].name + '</option>'); } else { teamDropDownText.push('<option value="' + teams[j].teamId + '">' + teams[j].name + '</option>'); } } $('#addIncidentTeam').html(teamDropDownText.join('')); $('#editIncidentTeam').html(teamDropDownText.join('')); /* When all this has finished, make the rest of the calls to get the rest of the data */ startTimer(); downloadDevices(); initializeMap(); var kmlLink = document.getElementById('kmlLink'); var geoserverLink = document.getElementById('geoserverLink') if (user_role.substring(0, 1) == 'M') { kmlLink.href = "https://www.intelink.gov/giatstldni/hermes/webservice/kml/download/M&" + thisTeam.missionId + "&48"; kmlLink.innerHTML = "https://www.intelink.gov/giatstldni/hermes/webservice/kml/download/M&" + thisTeam.missionId + "&48"; geoserverLink.href = "https://www.intelink.gov/giatstldni/geoserver/wms/kml?layers=hermes_all&cql_filter=mission_id+=+" + thisTeam.missionId; geoserverLink.innerHTML = "https://www.intelink.gov/giatstldni/geoserver/wms/kml?layers=hermes_all&cql_filter=mission_id+=+" + thisTeam.missionId;} else { kmlLink.href = "https://www.intelink.gov/giatstldni/hermes/webservice/kml/download/T&" + thisTeam.id + "&48"; kmlLink.innerHTML = "https://www.intelink.gov/giatstldni/hermes/webservice/kml/download/T&" + thisTeam.id + "&48"; geoserverLink.href = "https://www.intelink.gov/giatstldni/geoserver/wms/kml?layers=hermes_all&cql_filter=team_id+=+" + thisTeam.id; geoserverLink.innerHTML = "https://www.intelink.gov/giatstldni/geoserver/wms/kml?layers=hermes_all&cql_filter=team_id+=+" + thisTeam.id; } } }

我这样做只是为了看看我会得到什么错误,我知道它无法正常工作。但现在它告诉我文件中不存在的行有错误!我明白了:

  

语法错误   [打破此错误](10超出范围8)

我去了评论更多的代码,它只是让它超出范围6!我不明白!

1 个答案:

答案 0 :(得分:0)

我找到了罪魁祸首。返回的JSON值之一是空的(没有引号或任何内容)。不是一个非常有用的错误消息。