且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重用XMLHtt prequest对象或创建一个新的?

更新时间:2023-11-30 14:16:40

您误解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 $ C函数$ C>