2009-02-26 31 views
2

我想決定是否使用自定義ASP.Net Ajax Extender或jQuery來執行簡單的Web服務調用。 Web服務方法接受客戶ID並返回客戶名稱。我傾向於jQuery,因爲它很簡單。唯一的問題是,由於我公司的IE7的組策略設置,首次jQuery的調用它提示用戶以下消息Web服務:如何使用JavaScript來抑制IE7 ActiveX消息?

一個腳本訪問某些軟件 (ActiveX控件)上此頁面已被標記爲 腳本安全 。你想允許這個嗎?

擴展器不會導致顯示此消息。我假設ASP.Net Ajax庫有一些壓制它的javascript voodoo。所以我的問題是,如何使用javascript壓縮此消息?

這裏是我的ASPX標記:

<h1> 
    Finder Test</h1> 
<div> 
    <h2> 
     Extender</h2> 
    Customer ID: 
    <asp:TextBox ID="txtCustomerId" runat="server" MaxLength="9" Width="4em" /> 
    <belCommon:FinderExtender ID="extCustomerId" runat="server" TargetControlID="txtCustomerId" 
     ResultLabelID="lblResult" ServicePath="~/Customer.asmx" ServiceMethod="Name" /> 
    <asp:Label ID="lblResult" runat="server" /> 
</div> 
<div> 
    <h2> 
     jQuery</h2> 
    Customer ID: 
    <input id="txtCustomerId2" type="text" maxlength="9" style="width: 4em;" value="0000" /> 
    <span id="txtCustomerName2"></span> 

    <script type="text/javascript"> 
     $(document).ready(function() 
     { 
      $("#txtCustomerId2").change(
      function() 
      { 
       updateCustomerDescription(this.value, "txtCustomerName2"); 
      }).change(); 
     }); 

     function updateCustomerDescription(id, descriptionControlId) 
     { 
      // if we don't have a value, then don't bother calling the web service 
      if (id == null || id.length == 0) 
      { 
       $("#" + descriptionControlId).text(""); 
       return; 
      } 

      jsonAjax("customer.asmx/Name", "{'id':'" + id + "'}", true, 
       function(result) 
       { 
        var name = result.d == null ? "" : result.d; 
        $("#" + descriptionControlId).text(name); 
       }, null); 
     } 

     function jsonAjax(url, data, async, onSuccess, onFailed) 
     { 
      $.ajax({ 
       async: async, 
       type: "POST", 
       url: url, 
       data: data, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: onSuccess, 
       error: onFailed 
      }); 
     } 
    </script> 
</div> 

[更新]

我假設該消息中引用的ActiveX控件的XMLHttpRequest。我還假設jQuery和ASP.Net Ajax的內部都將它用於IE7。

[更新]

的區別似乎是在ASP.Net的Ajax和jQuery如何構造的XMLHttpRequest的一個實例。

ASP.Net Ajax的(感謝@Jesse迪林):

window.XMLHttpRequest = function window$XMLHttpRequest() { 
var progIDs = [ 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP' ]; 
for (var i = 0, l = progIDs.length; i < l; i++) { 
    try { 
    return new ActiveXObject(progIDs[i]); 
    } 
    catch (ex) { } 
    } 
    return null; 
    } 
} 

的jQuery 1.3.2:

// Create the request object; Microsoft failed to properly 
// implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available 
// This function can be overriden by calling jQuery.ajaxSetup 
xhr:function(){ 
    return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); 
} 

回答

1

我解決了這個通過重寫jQuery的XHR功能如此:

function overrideJqueryXhr() 
{ 
    $.ajaxSetup({ 
     xhr: function() 
     { 
      if (window.XMLHttpRequest) 
      { 
       return new XMLHttpRequest(); 
      } 
      else 
      { 
       var progIDs = ['Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP']; 

       for (var i = 0; i < progIDs.length; i++) 
       { 
        try 
        { 
         var xmlHttp = new ActiveXObject(progIDs[i]); 
         return xmlHttp; 
        } 
        catch (ex) 
        { 
        } 
       } 

       return null; 
      } 
     } 
    }); 
} 

這個函數告訴jQuery來創建用於非IE瀏覽器的XMLHttpRequest類的一個實例,然後創建一個ActiveX對象的MS Ajax的方式爲IE。它首先嚐試最新版本,Msxml2.XMLHTTP.3.0,然後是Msxml2.XMLHTTP,最後是Microsoft.XMLHTTP。

2

這聽起來不是我的權利。如果存在安全策略,則不應該使用JavaScript覆蓋它。

我會仔細檢查兩頁,看看他們是否真的訪問相同的ActiveX控件。我假設這些頁面來自同一個主機(所以沒有不同的WinInet信任區域的問題)。

+0

ASP.Net Ajax和jQuery實現都在同一個頁面上,只有jQuery方法導致消息。我很確定這兩個實現都使用了一個XMLHttpRequest ActiveX對象。 – jrummell 2009-02-26 17:54:17

+0

我會看看頁面的來源,而不是做一個假設。 – jdigital 2009-02-26 18:56:54

1

我工作的ASP.NET AJAX應用程序,所以我檢查它的來源,我發現這個在ASP.NET AJAX代碼:

window.XMLHttpRequest = function window$XMLHttpRequest() { 
var progIDs = [ 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP' ]; 
for (var i = 0, l = progIDs.length; i < l; i++) { 
    try { 
    return new ActiveXObject(progIDs[i]); 
    } 
    catch (ex) { } 
    } 
    return null; 
    } 
} 

我希望幫助一些。