jQuery AJAX 请求在 IE8 中失败,并显示消息“错误:调用 open 方法之前无法调用此方法。”

发布于 2024-10-09 23:31:37 字数 1051 浏览 7 评论 0原文

我正在使用 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/psxhttp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2024-10-16 23:31:37

好吧,这是修复!该请求使用了 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();
                }
        });

以下是我找到解决方案的方法:

  1. 更新到 jQuery 1.4.4,以防问题是已修复的错误。
  2. 单步调试 Firebug 调试器和 DevTools 调试器,直到结果似乎截然不同。
  3. 在第 5899 行,ajax() 函数使用 xhr() 函数创建 XmlHttpRequest 对象。在 Firefox 中,它返回了良好的数据。在 IE 中,返回的所有字段均为错误:调用 open 方法之前无法调用此方法。
  4. 我在第 5749 行分析了此函数,return new window.XMLHttpRequest() ;
  5. 我在 Google 上搜索并发现了这个页面有同样的问题并提出了适合我的解决方案。
  6. 官方 jQuery 票证

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 to window.ActiveXObject("Microsoft.XMLHTTP") as it should.

Add this to your script somewhere before your AJAX call (only verified in IE8, not other IE's):

jQuery.ajaxSetup({
            xhr: function() {
                    //return new window.XMLHttpRequest();
                    try{
                        if(window.ActiveXObject)
                            return new window.ActiveXObject("Microsoft.XMLHTTP");
                    } catch(e) { }

                    return new window.XMLHttpRequest();
                }
        });

Here's how I came to the solution:

  1. Updated to jQuery 1.4.4 in case the issue was a bug that had been fixed.
  2. Stepped through Firebug debugger and DevTools debugger until the results seemed to be drastically different.
  3. On line 5899, the ajax() function creates the XmlHttpRequest object with the xhr() function. In Firefox, it was returning good data. In IE, this was returning with all fields being Error: This method cannot be called until the open method has been called.
  4. I analyzed this function on line 5749, return new window.XMLHttpRequest();
  5. I googled and came across this page that has the same problem and suggested the solution that works for me.
  6. Official jQuery ticket:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文