0

我从事 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;萤火虫中给了我一个语法错误。我反复阅读我的代码,但无法弄清楚我做错了什么,所以我将分号放在同一行thisTeam.zoomLevel = data.team.zoomLevel,它告诉我它在空白行上有语法错误!

为了进行另一项测试,我将整个函数移到它自己的 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; } } }

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

语法错误 [Break On This Error] (10 out of range 8)

我去注释了更多代码,它只是使它超出了范围 6 的 10!我不明白!

4

1 回答 1

0

我找到了罪魁祸首。返回的 JSON 值之一是空的(没有引号或任何东西)。不是很有帮助的错误信息。

于 2012-11-20T20:02:53.417 回答