IE全局var arrayName.className为null

时间:2012-10-02 11:09:05

标签: javascript jquery internet-explorer

我有一个jQuery ajax调用,它填充数组;这个数组应该可以在另一个函数中访问。它实际上是在FireFox和Safari中,但不在IE中。 IE说:SCRIPT5007无法获取属性'name'的值:object为null或undefined

它看起来像'globalDataArray [i] .name'以及'globalDataArray [i] .objectid'有问题。两者都经过FF和IE的完美评估和使用,所以并不是空的。有人为什么会发生这种情况?我google了很多;最后使用逗号或东西的常见问题不是解决方案。

这里设置了var:

var globalDataArray = [];
function retrieveContentData(content){


    $.ajax({
        url: 'http://services.arcgis.com/nSZVuSZjHpEZZbRo/ArcGIS/rest/services/NLCito/FeatureServer/0/query',
        data: {
            where: content,
            geometryType: 'esriGeometryEnvelope',
            spatialRel: 'esriSpatialRelIntersects',
            outFields: '*',
            returnGeometry: false,
            returnIdsOnly: false,
            returnCountOnly: false,
            f: 'pjson'
        },
        success: function(data){
            data = $.parseJSON(data);//Always parse JSON data
            var features = data.features;
            for (var i=0; i<features.length; i++) {
                //globalDataArray.push(features[i].attributes.NAME);
                //globalDataArray[features[i].attributes.OBJECTID] = features[i].attributes.NAME;
                globalDataArray[i] = {  "objectid": features[i].attributes.OBJECTID,
                                        "name": features[i].attributes.NAME,
                                        "type": features[i].attributes.Type
                                        };
            }
            shuffle(globalDataArray);//Shuffle the array items

            //Count total and set progress report
            $('#totalTasks').text(features.length);

            //Initialize the progress bar and create the first task
            updateProgressBar(0);
            createNewTask();

        }//End success
    });//End Ajax call

}//End function

这是我想再次使用它的地方:

function validateAnswer(){
    //Prevent validating if task div not shown
    if($('#task').is(":visible")){

        var passedTask = false;

        var typedAnswer = $('#taskAnswerInput').val();

        var desiredAnswer       = globalDataArray[i].name;
        var desiredAnswerShort  = desiredAnswer.replace(/\(.*?\)/, "");//Remove eveything within and with bracklets
        desiredAnswerShort      = jQuery.trim(desiredAnswerShort);//Remove any whitespace on beginning and end of the string

        if(typedAnswer === desiredAnswer || typedAnswer === desiredAnswerShort){
            alert('Exact, helemaal goed!');
            $('#tasksRight').text(parseInt($('#tasksRight').text()) +1);
            passedTask = true;
            updateProgressBar(i);
        }else{
            alert('Jammer, dat is niet het goede antwoord');
        }

        if(passedTask == true){
            nextTask();
        }
    }//end if visible
}

结束这是调用createNewTask()函数的地方:

var i = 0;
function createNewTask(){
    //Since a new tasks is started, let's update the progress
    $('#tasksDone').text(i);

    //Highlight a single place
    executeQuery(globalDataArray[i].objectid);

    //Change tasks text
    var type = globalDataArray[i].type;
    if(type === 'Plaats'){ type = 'Welke plaats';}
    if(type === 'Gebied'){ type = 'Welk gebied';}
    if(type === 'Water'){ type = 'Welk water';}
    if(type === 'Provincie'){ type = 'Welke provincie';}
    $('#taskPointType').html(type);

    $('#taskAnswerInput').val('');//Clear the input field
}

function giveupTask(){
    var correctAnswer = globalDataArray[i].name;
    alert(correctAnswer);
    $('#tasksWrong').text(parseInt($('#tasksWrong').text()) +1);//Update currentWrong
    nextTask();
}

//Aparte functie, om validateAnswer() flexibeler te houden
function nextTask(){
    //fire new task
    i++;
    if(i < globalDataArray.length){
        //Update progressbar
        updateProgressBar(i+1);//+1 since i starts with 0
        createNewTask();
    }else{
        //All tasks done
        alert('Einde, alle plaatsen gehad');
    }
}

1 个答案:

答案 0 :(得分:0)

解决了它。对于任何感兴趣的人:

看起来IE并不总是给出正确的错误,jquery ajax成功,完整和错误功能,我们有助于修复它。 最后,事实证明问题出在跨域ajax调用中; IE正在阻止它们,除非你使用的是dataType:jsonp。只是普通的json不够好。

感谢您分享您的想法!