检索跨浏览器 XmlHttpRequest 的最简单方法 [英] Easiest way to retrieve cross-browser XmlHttpRequest

查看:31
本文介绍了检索跨浏览器 XmlHttpRequest 的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检索适用于所有浏览器的 XmlHttpRequest 对象的最简单和最安全的方法是什么?没有任何额外的库.有没有你经常使用的代码片段?

What is the easiest and safest way to retrieve XmlHttpRequest object that works across all browsers? Without any extra libraries. Is there a code snippet you use often?

附言我知道网络上有大量的例子,但这正是我问的原因:有太多不同的例子,我只想要一些简单且行之有效的东西.

P.S. I know there are tons of examples on the net, but this is precisely the reason I am asking: there are too many different examples, and I just want something simple and proven to work.

jQuery 和其他库不是一种选择.为什么 jquery 内存泄漏如此严重?

推荐答案

虽然我建议使用完整的库来简化使用,但在现代浏览器中创建 AJAX 请求可能相当简单:

While I would recommend using a full library to make usage easier, making AJAX requests can be fairly simple in modern browsers:

var req = new XMLHttpRequest();
req.onreadystatechange = function(){
    if(this.readyState == 4){
        alert('Status code: ' + this.status);
        // The response content is in this.responseText
    }
}
req.open('GET', '/some-url', true);
req.send();

以下代码段是基于 quirksmode.org 的代码段的更高级的代码段甚至支持非常旧的浏览器(比 Internet Explorer 7 旧):

The following snippet is a more advanced snippet based on a snippet from quirksmode.org and even supports very old browsers (older than Internet Explorer 7):

function sendRequest(url,callback,postData) {
    var req = createXMLHTTPObject();
    if (!req) return;
    var method = (postData) ? "POST" : "GET";
    req.open(method,url,true);
    // Setting the user agent is not allowed in most modern browsers It was
    // a requirement for some Internet Explorer versions a long time ago.
    // There is no need for this header if you use Internet Explorer 7 or
    // above (or any other browser)
    // req.setRequestHeader('User-Agent','XMLHTTP/1.0');
    if (postData)
        req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    req.onreadystatechange = function () {
        if (req.readyState != 4) return;
        if (req.status != 200 && req.status != 304) {
//          alert('HTTP error ' + req.status);
            return;
        }
        callback(req);
    }
    if (req.readyState == 4) return;
    req.send(postData);
}

var XMLHttpFactories = [
    function () {return new XMLHttpRequest()},
    function () {return new ActiveXObject("Msxml3.XMLHTTP")},
    function () {return new ActiveXObject("Msxml2.XMLHTTP.6.0")},
    function () {return new ActiveXObject("Msxml2.XMLHTTP.3.0")},
    function () {return new ActiveXObject("Msxml2.XMLHTTP")},
    function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i=0;i<XMLHttpFactories.length;i++) {
        try {
            xmlhttp = XMLHttpFactories[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
}

这篇关于检索跨浏览器 XmlHttpRequest 的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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