Best method of Instantiating an XMLHttpRequest object

前端 未结 9 1469
一生所求
一生所求 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 21:00

    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();
    

提交回复
热议问题