7

What is the best method for creating an XMLHttpRequest object?

It should work in all capable browsers.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user51886
  • 332
  • 3
  • 8
  • As an additional info, this topic will also be useful: [JavaScript: Which should I use, Microsoft.XMLHTTP or Msxml2.XMLHTTP?](http://stackoverflow.com/q/1919582/814702) – informatik01 Oct 22 '13 at 11:57

9 Answers9

11

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;
}
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Clean and spartan. Might want to "remember" the located version so you don't have to hit 1-4 exceptions every time. – Nicholas Jan 06 '09 at 10:55
9

Here's a useful link and some code (should cover all bases)

http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx

        var request = null;

        function InitAJAX()
        {
            var objxml = null;
            var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"];            

            try
            {
                objxml = new XMLHttpRequest();
            }
            catch(e)
            {                
                for (var i = 0; i < ProgID.length; i++)
                {
                    try
                    {
                        objxml = new ActiveXObject(ProgID[i]);
                    }
                    catch(e)
                    {                        
                        continue;
                    }
                }
            }

            return objxml;            
        }

        request = InitAJAX();
user50612
  • 3,098
  • 3
  • 25
  • 20
  • 2
    @Bishiboosh - It works fine in Firefox - https://developer.mozilla.org/En/Using_XMLHttpRequest –  Jan 14 '10 at 15:21
8

Use jQuery (or a similar JavaScript library). It takes care of the cross-browser compatibility issues of things like making Ajax calls.

For example, using the jQuery Ajax call:

$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cletus
  • 616,129
  • 168
  • 910
  • 942
8

I'd suggest following Sergey's advise or writing a small, less sophisticated patch for IE yourself:

if(typeof window.XMLHttpRequest === 'undefined' &&
    typeof window.ActiveXObject === 'function') {
    window.XMLHttpRequest = function() {
        try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
        try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
        return new ActiveXObject('Microsoft.XMLHTTP');
    };
}

Then you can do

var req = new XMLHttpRequest;

even in IE.

edit 2011-02-18: see this blogpost for the rationale behind the new choice of MSXML versions...

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Christoph
  • 164,997
  • 36
  • 182
  • 240
3

Using just about any JavaScript Ajax library is preferable to writing your own Ajax framework -- unless that's the point. You might want to check out the jQuery or Prototype or MooTools or Dojo or [insert name here] framework to see how they do it if you insist on writing your own.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
1

This is what I use, it works fine for me:

    function request()
    {
        try
        {
            try
            {
                return new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch( e )
            {
                return new ActiveXObject("Msxml2.XMLHTTP")
            }
        }
        catch(e) 
        {
            return new XMLHttpRequest()
        }
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hasen
  • 161,647
  • 65
  • 194
  • 231
1

Use XMLHttpRequest.js - Standard-compliant cross-browser XMLHttpRequest object implementation and work with the object in a standard (W3C) way

Sergey Ilinsky
  • 31,255
  • 9
  • 54
  • 56
0

I go with Cletus's suggestion of jQuery and also check out the jQuery Form plug-in, very powerful and simple to use to quickly convert your forms to work via Ajax.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Slee
  • 27,498
  • 52
  • 145
  • 243
0

function CreateXmlHttpObj() {

try {
    XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
    try {
        XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (oc) {
        XmlHttpObj = null;
    }
}
// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
if (!XmlHttpObj && typeof XMLHttpRequest != "undefined") {
    XmlHttpObj = new XMLHttpRequest();
}

}

DSharper
  • 3,177
  • 9
  • 29
  • 47