顺序调用上的 jQuery 自动完成在 IE7 独立上抛出 JS 错误

发布于 2024-11-28 22:14:10 字数 475 浏览 2 评论 0原文

我们有一个文本框与 jQuery UI Autocomplete v1.8.14 相结合,

它基本上工作得很好并且没有错误,除了 IE7 独立中的某些情况之外。假设用户正在输入一个字母,并且第一个 AJAX 请求发出以获取自动完成器结果。响应尚未返回,但用户输入了另一个字母。此时,第一个请求将默认中止,并启动第二个请求以检索新搜索字符串的新结果。这是有道理的,因为它节省了带宽并损失了执行时间,但不幸的是它在 IE7 上失败了。

下图显示了在 Firefox 中使用 Firebug 会发生什么情况。基本上所有浏览器都可以正常工作,除了 IE7(甚至 IE6 和 IE8)。

显示 Firebug 中行为的附加图像

有人见过这样的东西吗?最终有人知道是否可以强制自动完成程序完成已启动的请求而不是中止它们?还有其他想法吗?

非常感谢,非常感谢任何帮助!

We have a textbox coupled with jQuery UI Autocomplete v1.8.14

It basically works great and without bugs except a certain scenario in IE7 standalone. Let's assume that a user is inputting a letter and the first AJAX request goes out for the autocompleter results. The response isn't yet back but the user inputs another letter. At this point the first request is being aborted be default and a second one is started to retrieve the new results for the new search string. This makes sense as is spares bandwidth and lost execution time but unfortunately it fails on IE7.

The following image is showing what happens using Firebug in Firefox. Essentially all browsers work correctly except IE7 (even IE6 and IE8).

Attached image showing the behavior in Firebug

Did anybody ever see something like this? Does eventually somebody know if it's possible to force the autocompleter to finish the started requests instead of aborting them? Any other ideas?

Thanks a lot, any help is highly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

人生百味 2024-12-05 22:14:10

我有预感您只是使用 source 选项的字符串值并为其提供一个 URL。如果 AJAX 请求正在进行,您关于小部件中止 AJAX 请求的说法是正确的。 查看此代码段小部件的源代码:

url = this.options.source;
this.source = function(request, response) {
    if (self.xhr) {
        self.xhr.abort(); // <-- Problematic line.
    }
    self.xhr = $.ajax({
        url: url,
        data: request,
        dataType: "json",
        autocompleteRequest: ++requestIndex,
        success: function(data, status) {
            if (this.autocompleteRequest === requestIndex) {
                response(data);
            }
        },
        error: function() {
            if (this.autocompleteRequest === requestIndex) {
                response([]);
            }
        }
    });
}

如果您将字符串传递给 source 参数,您可以轻松地将其替换为手动执行 AJAX 调用的函数(并且不会中止请求):

source: function (request, response) {
    $.ajax({
        url: "your_url",
        data: request.term,
        dataType: "json",
        success: function(data, status) {
            response(data);
        }
    });
}

(未经测试,但应该让您朝着正确的方向前进)

I have a hunch you're just using the string value to the source option and supplying it a URL. You are correct about the widget aborting AJAX requests if one is in progress. Check out this snippet from the widget's source code:

url = this.options.source;
this.source = function(request, response) {
    if (self.xhr) {
        self.xhr.abort(); // <-- Problematic line.
    }
    self.xhr = $.ajax({
        url: url,
        data: request,
        dataType: "json",
        autocompleteRequest: ++requestIndex,
        success: function(data, status) {
            if (this.autocompleteRequest === requestIndex) {
                response(data);
            }
        },
        error: function() {
            if (this.autocompleteRequest === requestIndex) {
                response([]);
            }
        }
    });
}

If you are passing a string to the source parameter, you can easily replace that with a function that does the AJAX call manually (and that you don't abort the request in):

source: function (request, response) {
    $.ajax({
        url: "your_url",
        data: request.term,
        dataType: "json",
        success: function(data, status) {
            response(data);
        }
    });
}

(Untested, but should get you headed in the right direction)

逆光飞翔i 2024-12-05 22:14:10

我们最终通过将调用放入 try-catch 块中解决了这个问题,并在 IE7 中触发时忽略错误。

这可能不是完美的方法,但中止请求的好处对于其他浏览器仍然有效。

We finally solved the issue by putting the call into a try-catch block and ignore the error if fired in IE7.

It's probably not the perfect way to do it, but the benefits of aborting the request remain valid for the rest of the browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文