我想决定是否使用自定义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();
}
ASP.Net Ajax和jQuery实现都在同一个页面上,只有jQuery方法导致消息。我很确定这两个实现都使用了一个XMLHttpRequest ActiveX对象。 – jrummell 2009-02-26 17:54:17
我会看看页面的来源,而不是做一个假设。 – jdigital 2009-02-26 18:56:54