Best method of Instantiating an XMLHttpRequest object

前端 未结 9 1350
一生所求
一生所求 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:08

    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()
            }
        }
    
    0 讨论(0)
  • 2020-12-31 21:12

    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
        }
    });
    
    0 讨论(0)
  • 2020-12-31 21:12

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

    0 讨论(0)
提交回复
热议问题