2012-03-09 3 views
0

다음 스크립트는 Safari, Chrome 및 Firefox에서 제대로 실행되고 작동하지만 IE8에서는 작동하지 않습니다. 불행히도 IE8은 내 타겟 브라우저 중 하나이기 때문에 약간 문제가 있습니다. 아약스에 대한 많은 경험이 없기 때문에 어디서부터 시작해야할지 모르겠습니다.IE8에서 Ajax 스크립트가 실패합니다.

나는 IE가 라인 15 (**로 표시)에 오류를보고하는데, 실제로는 그 라인을 보지 못하도록 if-else가 이해하지 못한다고 지적했다.

function getNames(str) { 
    var xmlhttp; 
    // Clear previous queries 
    if(str.length == 0){ 
     document.getElementById("txtHint").innerHTML = ""; 
     return; 
     // Due to the high number of possible hits we'll demand 3 chars 
     // before we start querying. 
    }else if(str.length < 3){ 
     return ; 
    } 
    if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp = new XMLHttpRequest(); 
    }else{ // code for IE6, IE5 
     **xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");** 
    } 
    xmlhttp.onreadystatechange = function(){   
     if(xmlhttp.status == 200 && xmlhttp.readyState == 4){ 
      // String from get_names.php is comma separated 
      var arr = xmlhttp.responseText.split(","); 
      // The UL list we want our names in 
      var ul = document.getElementById("names"); 

      // Clear the list for each key in 
      if(ul.hasChildNodes()){ 
       while(ul.childNodes.length >= 1){ 
        ul.removeChild(ul.firstChild); 
       } 
      } 
      // Step trough the String in Array form 
      for(var i = 0; i < arr.length; i++){ 
       // :@ means that we've reached the end of applicable names. 
       if (arr[i] != ":@") { 
        var li = document.createElement("li"); 
        li.innerHTML = newListItem = arr[i]; 
        // Inserts the current name into the list. 
        ul.insertBefore(li, ul.getElementsByTagName("li")[0]); 
       } 
      } 
     } 
    } 
    xmlhttp.open("GET", "./ext/get_names.php?q=" + str, true); 
    xmlhttp.send(); 
} 
+0

답변 : jQuery와 같은 프레임 워크를 사용해 보셨습니까? 그들은 당신을 위해 모든 크로스 브라우저 헨 니 간을 돌보아줍니다. – Jamiec

+0

질문에 대한 직접적인 대답은 아니지만 js 프레임 워크 (jQuery/mootools/...)를 고려해야합니다. 이것들은 크로스 플랫폼에서 작동하고 두통을 덜어줍니다. –

+0

활성 x 객체가 사용 가능한지 테스트하기 위해 로직을 추가 할 수 있습니까? (if (window.ActiveXObject)) –

답변

0

은 먼저 상태를 확인 다음 readyState가와 을 확인해야합니다. 이것은 대부분의 브라우저에서보고되지만 무시되는 일반적인 오류입니다. 이 문제가 해결되었는지 확신 할 수 없지만 오류 메시지를 제공하지 않았기 때문에 더 이상 도움을 줄 수 없습니다.

if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
    // Code ... 
} 
+0

왜 순서가 중요합니까? READING *'readyState'가'status'에 * 부작용 *을 일으키지 않는 한 ...// (단지 논리적 인 접속 :'&&'에주의하십시오.) –

+0

속성 '상태'는 readState가 4 (또는 3) 일 때만 액세스 할 수 있습니다. readyState가이 값 중 하나가 아닌 변수에 액세스하면 Chrome 및 Firefox에서 오류가 발생합니다. – Saebekassebil

+0

그래, 왜'xmlhttp.readyState == 4 && xmlhttp.status == 200'이'xmlhttp.status == 200 && xmlhttp.readyState == 4' (게시물에서)와 다른가요? 부작용이 없다고 가정하면 좀 더 논리적 인 레이아웃이라는 것에 동의하지만 'a && b'와 'b && a'는 논리적으로 동일합니다. –

0

'window.XMLHttpRequest'-check는 확인해야합니다.

this 답을 살펴볼 수 있습니다. 이 경우 문제는 브라우저 설정에서 네이티브 xmlhttprequest가 비활성화 된 것입니다.

관련 문제