2012-10-02 30 views
1

我有一个jQuery ajax调用,它填充一个数组;这个数组应该可以在另一个函数中使用。它实际上是在FireFox和Safari中,但不在IE中。 IE说:SCRIPT5007无法获取属性'name'的值:对象为空或未定义IE全局var arrayName.className为空

它看起来像'globalDataArray [i] .name'以及'globalDataArray [i]'有问题。 OBJECTID”。 FF和IE都可以很好地评估和使用它们,所以它们并非真的是空的。任何有关为什么发生这种情况的想法我搜索了很多;在最后使用逗号或东西的常见问题不是解决方案。

这里变量被设置:

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'); 
    } 
} 
+0

Ajax是异步的。 – undefined

+0

如何在'validateAnswer()'中定义'i'? –

+0

validateAnswer()在哪里被调用?您可能会看到与同步相关的问题。 –

回答

0

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

它看起来像IE并不总是回馈正确的错误,jquery ajax成功,完整和错误的功能,我们有助于解决它。 最后发现问题出在跨域ajax调用中; IE会阻止它们,除非你使用dataType:jsonp。只是普通的json不够好。

感谢分享你的想法!