jQuery AJAX 请求在 IE8 中失败,并显示消息“错误:调用 open 方法之前无法调用此方法。”
我正在使用 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 回调。错误回调会发出以下消息警报:
Error: This method cannot be called until the open method has been called.
我的响应标头返回 Content-Type: text/javascript;字符集=UTF-8
。
IE 是怎么回事?我在本地主机上运行服务器,从 http://localhost:8080/psx 向 http://localhost:8080/helper。也许 IE 阻止了这个请求?我尝试安装 Fiddler 来分析请求流量,但它无法在我的计算机上运行,因为它已被锁定。 Firebug 让我这么做,但那里一切似乎都很好。
感谢您的帮助!
I'm using jQuery 1.4.2 and am trying to perform a simple AJAX request. The target URL returns a JSON string (I validated it with jslint). The request works in Firefox and Chrome, but doesn't want to work in IE8, and I can't determine why. Here is the call:
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 will execute the beforeSend callback and the error callback. The error callback alerts with the message:
Error: This method cannot be called until the open method has been called.
My response header returns with Content-Type: text/javascript; charset=UTF-8
.
What is going on with IE? I'm running the server on localhost, making a request from http://localhost:8080/psx to http://localhost:8080/helper. Maybe IE is blocking this request? I have tried installing Fiddler to analyze request traffic but it won't run on my machine because it's rather locked down. Firebug lets me, but everything seems good there.
Thanks for the help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这是修复!该请求使用了
window.XMLHttpRequest()
,由于某种原因,该请求在 IE8 中无法正常工作。 jQuery 不会像它应该的那样故障返回到window.ActiveXObject("Microsoft.XMLHTTP")
。在 AJAX 调用之前将其添加到脚本中(仅在 IE8 中验证,不在其他 IE 中验证):
以下是我找到解决方案的方法:
错误:调用 open 方法之前无法调用此方法。
return new window.XMLHttpRequest() ;
Alright, here's the fix! The request was using
window.XMLHttpRequest()
, which isn't working properly in IE8 for some reason. jQuery is not failing back towindow.ActiveXObject("Microsoft.XMLHTTP")
as it should.Add this to your script somewhere before your AJAX call (only verified in IE8, not other IE's):
Here's how I came to the solution:
Error: This method cannot be called until the open method has been called.
return new window.XMLHttpRequest();