我正在使用jQuery 1.4.2并尝试执行简单的AJAX请求。目标URL返回一个JSON字符串(我用jslint验证它)。该请求适用于Firefox和Chrome,但不想在IE8中工作,我无法确定原因。这是电话:
jQuery.ajax({
url: 'http://' + domain + '/' + 'helper/echo/',
dataType: 'json',
success: function(data) {
alert(data);
},
beforeSend: function(request, settings) {
alert('Beginning ' + settings.dataType + ' request: ' + settings.url);
},
complete: function(request, status) {
alert('Request complete: ' + status);
},
error: function(request, status, error) {
alert(error);
}
});
IE将执行beforeSend回调和错误回调。错误回调警告消息:
Error: This method cannot be called until the open method has been called.
我的回复标头返回Content-Type: text/javascript; charset=UTF-8
。
IE正在发生什么事?我正在localhost上运行服务器,从http://localhost:8080/psx向http://localhost:8080/helper发出请求。也许IE阻止了这个请求?我已经尝试安装Fiddler来分析请求流量,但它不会在我的机器上运行,因为它被锁定了。 Firebug让我,但一切似乎都很好。
感谢您的帮助!!!
答案 0 :(得分:14)
好的,这是修复!请求使用window.XMLHttpRequest()
,由于某种原因,它在IE8中无法正常工作。 jQuery没有失败回到window.ActiveXObject("Microsoft.XMLHTTP")
。
在AJAX调用之前将其添加到您的脚本中(仅在IE8中验证,而不是在其他IE中验证):
jQuery.ajaxSetup({
xhr: function() {
//return new window.XMLHttpRequest();
try{
if(window.ActiveXObject)
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(e) { }
return new window.XMLHttpRequest();
}
});
以下是我找到解决方案的方法:
Error: This method cannot be called until the open method has been called.
return new window.XMLHttpRequest();