2011-11-01 73 views
2

我面臨的問題是,每當我關閉IE8中的安全設置時,在我的應用程序中,xmlHTTPRequest調用失敗。但是當我打開它時,它的工作正常。 安全功能是:工具 - > Internet選項 - >高級 - >安全性 - >啓用本機xmlHTTP支持。 如果選中,則不會發生問題,但如果未檢查,則xmlHTTPReq無法向服務器發送請求(在調試器窗口中未看到cgi調用)。IE 8安全設置阻止JavaScript發送xmlHTTPRequest

所以我的問題是:是否有可能檢測到這個安全設置是否啓用或不使用JavaScript編程?

我對CGI調用代碼是根據:

try{ 
     var xmlHttpReq; 
     var destinationURL = cgiBinPath+"helloworld.cgi?start"; 
     // IE 7+, Mozilla/Safari 
     if (window.XMLHttpRequest) { 
      xmlHttpReq=new XMLHttpRequest(); 
     } 
     //IE 5,6 
     else { 
      xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     if (xmlHttpReq.readyState == 0){ 
      xmlHttpReq.open('GET', destinationURL, true); 
      xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
      xmlHttpReq.onreadystatechange = function(){ 
       if(xmlHttpReq.readyState == 4){ 
        //Control never reaches here 
       } 
      }    
      xmlHttpReq.send(); 
     } 
    }catch(e){ 
     alert('Exception '+e); 
    } 
+0

任何有理智的人禁用此設置 – ThiefMaster

回答

2

this文章有關MSDN本地XMLHTTP看看。它還爲您的問題提供瞭解決方案。

編輯 對不起,我沒有仔細閱讀文章... This一個會回答你的問題。如果可以創建XMLHttpRequest對象的實例,則必須嘗試。

if (window.XMLHttpRequest) { 
    try { 
     xmlHttpReq = new XMLHttpRequest(); 
    } catch (ex) { 
     xmlHttpReq = new window.ActiveXObject("Microsoft.XMLHTTP"); 
    } 
} else { 
    //... 
} 
+0

感謝您的答覆... 我已經研究了這個文章,但我覺得沒有什麼是有可能幫我檢測到特定的安全功能已啓用或不... – Ved

+0

@ adn295我編輯了我的答案 – Andreas

+0

再次感謝... !!! 我會嘗試這一個,會讓你知道,如果它解決了我的問題.. 一件事澄清,如果我直接寫在這樣的catch塊內容: - xmlHttpReq =新的ActiveXObject(「Microsoft.XMLHTTP 「); (沒有「窗口」)它是否與你的答案有任何區別? – Ved