2009-01-06 11 views

Respuesta

8

Use jQuery (o una biblioteca similar de JavaScript). Se encarga de los problemas de compatibilidad entre navegadores de cosas como hacer llamadas Ajax.

For example, utilizando el 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 
    } 
}); 
9

Aquí hay un enlace útil y algo de código (debe cubrir todas las 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(); 
+0

Cross-browser, ¿alguien? No creo que eso funcione en Firefox. –

+2

@Bishiboosh - Funciona bien en Firefox - https://developer.mozilla.org/En/Using_XMLHttpRequest –

3

Uso de casi cualquier Ajax biblioteca de JavaScript es preferible a escribir su propio marco Ajax, a menos que ese sea el punto. Es posible que desee consultar el marco jQuery o Prototype o o Dojo o [insertar nombre aquí] para ver cómo lo hacen si usted insiste en escribir el suyo.

11

Para una solución de biblioteca de menos, se puede emular el uso de Prototipo de Try.these con bastante facilidad:

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; 
} 
+0

Limpio y espartano. Es posible que desee "recordar" la versión localizada para que no tenga que presionar de 1 a 4 excepciones cada vez. – Nicholas

1

Esto es lo que yo uso, funciona muy bien para mí:

function request() 
    { 
     try 
     { 
      try 
      { 
       return new ActiveXObject("Microsoft.XMLHTTP") 
      } 
      catch(e) 
      { 
       return new ActiveXObject("Msxml2.XMLHTTP") 
      } 
     } 
     catch(e) 
     { 
      return new XMLHttpRequest() 
     } 
    } 
0

voy con La sugerencia de Cletus de jQuery y también verifica el jQuery Form plug-in, muy poderoso y simple de usar para convertir rápidamente tus formularios al trabajo a través de Ajax.

8

te sugiero siguiente Sergey's advise o escribiendo un pequeño parche, menos sofisticado para IE mismo:

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'); 
    }; 
} 

entonces usted puede hacer

var req = new XMLHttpRequest; 

incluso en IE.

edición 2011-02-18: ver this blogpost para el lema de la nueva elección de versiones MSXML ...

+0

el enlace es genial, que arregla el objeto – Delta

0

función 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(); 
} 

}

Cuestiones relacionadas