Rob*_*ell 4 jquery xmlhttprequest
将鼠标悬停在jcarousel中的图像上时,我的网站会显示一个弹出窗口,其内容通过ajax加载.我正在做我认为相当简单的事情; 保留xhrRequest对象的句柄,并在发出新请求之前中止现有对象.
它在除IE之外的所有浏览器中都很好用,我收到错误"对象不支持此属性或方法"
这是触发它的代码:
function showPopup {
// ... code snipped ...
// cancel the existing xhr request
if (showPopup.xhrRequest != null) {
showPopup.xhrRequest.abort();
showPopup.xhrRequest = null;
}
showPopup.xhrRequest = $.ajax({url: url,
type: "GET",
success:function(data) {
$("#popup-content").html(data);
}
});
// ... code snipped ...
}
showPopup.xhrRequest = null;
Run Code Online (Sandbox Code Playgroud)
适用于Firefox和Chrome.我将错误追溯到ajax函数内的jquery.js中的这段代码(我的jQuery副本中的第5233行):
// Override the abort handler, if we can (IE doesn't allow it, but that's OK)
// Opera doesn't fire onreadystatechange at all on abort
try {
var oldAbort = xhr.abort;
xhr.abort = function() {
if (xhr ) {
oldAbort.call( xhr );
}
onreadystatechange( "abort" );
} catch(e) { }
Run Code Online (Sandbox Code Playgroud)
oldAbort.call(xhr)行发生特定错误.有任何想法吗?
在jquery论坛上找到了答案.请参阅此处的讨论:http: //forum.jquery.com/topic/object-doesn-t-support-this-property-or-method-from-jquery-1-4-1-in-ie7-only
IE7及以下的解决方法:
$.ajax({
...
xhr: function() {
if ($.browser.msie && $.browser.version.substr(0,1) <= 7)
return new ActiveXObject("Microsoft.XMLHTTP");
else
return new XMLHttpRequest();
},
...
});
Run Code Online (Sandbox Code Playgroud)