Best method of Instantiating an XMLHttpRequest object

前端 未结 9 1487
一生所求
一生所求 2020-12-31 20:43

What is the best method for creating an XMLHttpRequest object?

It should work in all capable browsers.

9条回答
  •  暖寄归人
    2020-12-31 20:50

    For a library-less solution, you can emulate Prototype's use of Try.these fairly easily:

    function newAjax() {
        try { return new XMLHttpRequest();                    } catch(){}
        try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(){}
        try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(){}
        try { return new ActiveXObject('Msxml2.XMLHTTP');     } catch(){}
        try { return new ActiveXObject('Microsoft.XMLHTTP');  } catch(){}
        return false;
    }
    

提交回复
热议问题