重用XMLHtt prequest对象或创建一个新的? [英] Reuse XMLHttpRequest object or create a new one?

查看:246
本文介绍了重用XMLHtt prequest对象或创建一个新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索计算器,但得到矛盾的答案:

I searched stackoverflow but got contradictory answers:

我为什么要重用XmlHtt prequest对象?

<一个href="http://stackoverflow.com/questions/11079543/ajax-intensive-page-reuse-the-same-xmlhtt$p$pquest-object-or-create-new-one-ever">Ajax-intensive页面:重复使用相同的XMLHtt prequest对象或创建新的每次

此外,还有在w3schools.com的建议:

Also, there's a recommendation on w3schools.com :

如果您有多个在您的网站1 AJAX的任务,你应该创建   一个标准功能,用于创建XMLHtt prequest对象,调用   这对于每一个AJAX的任务。

If you have more than one AJAX task on your website, you should create ONE standard function for creating the XMLHttpRequest object, and call this for each AJAX task.

为什么这个建议?我使用的是全球XMLHtt prequest对象在我的网页来处理所有的Ajax任务吧。

Why this recommendation? I'm instead using a global XMLHttpRequest object on my page for handling all Ajax tasks.

推荐答案

您误解W3School的建议。我还是要强调的相关部分:

You misunderstood W3School's recommendation. I'll highlight the relevant part:

如果您有多个AJAX任务在你的网站,你应该建立一个标准的功能作为创建XMLHtt prequest对象,并把这种对每个AJAX的任务。

If you have more than one AJAX task on your website, you should create ONE standard function for creating the XMLHttpRequest object, and call this for each AJAX task.

它说,你使用一个AJAX功能来获取请求。该功能将处理IE和其他浏览器之间的不一致。 XMLHtt prequest 在IE浏览器中的标准兼容的浏览器,而的ActiveXObject

It says that you use one AJAX function to fetch requests. This function will deal with the inconsistencies between IE and other browsers. XMLHttpRequest in standard-compliant browsers, and ActiveXObject in IE.

我建议使用多个XHR对象。随着一个全局XHR对象,你的应用程序只能处理在给定时间一个请求。这也是容易出错(如 XMLHtt prequest启动多次没有启动的onreadystatechange功能)。

I recommend to use multiple XHR objects. With one global xhr object, your application can only deal with one request at a given time. It's also error-prone (eg. XMLHttpRequest launches multiple times without initiating the onreadystatechange function).

W3Schools的意思是这样的:

W3schools meant something like:

function createXHR() {
    try {
        return new XMLHttpRequest();
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
}
var xhr = createXHR();
xhr.open('get', '/test', true);
xhr.send();

尽管这是更好地创建一个处理请求,如 jQuery.ajax

这篇关于重用XMLHtt prequest对象或创建一个新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆