1

我有一个填充数组的 jQuery ajax 调用;这个数组应该可以在另一个函数中访问。它实际上在 FireFox 和 Safari 中,但不在 IE 中。IE 说: SCRIPT5007 无法获取属性“名称”的值:对象为空或未定义

看起来“globalDataArray[i].name”和“globalDataArray[i].objectid”都有问题。两者都被 FF 和 IE 完美评估和使用,所以没有真正的空白。有人对为什么会发生这种情况有任何想法吗?我用谷歌搜索了很多;最后使用逗号或其他东西的常见问题不是解决方案。

这里设置了 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
}

End 这是调用 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');
    }
}
4

1 回答 1

0

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

看起来 IE 并不总是返回正确的错误、jquery ajax 成功、完整和错误函数,我们有助于修复它。最后发现问题出在跨域ajax调用上;IE 正在阻止它们,除非您使用的是 dataType: jsonp。仅仅普通的 json 是不够的。

感谢您分享您的想法!

于 2012-10-04T11:59:16.503 回答