2008-10-14 20 views
6

Estoy escribiendo un pequeño marco de Ajax para la reutilización en pequeños proyectos y he encontrado un problema. Básicamente recibo un error 'NS_ERROR_ILLEGAL_VALUE' al enviar la solicitud y no tengo idea de lo que está sucediendo.¿Cómo evitar un error 'NS_ERROR_ILLEGAL_VALUE' usando Ajax?

La página HTML (que se corta, pero muestra el error)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Ajax Test</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

     <script type="text/javascript"> 

      var COMPLETE = 4; 
      var OK = 200; 

      function GetXMLHttpRequestObject() 
      { 
       var XMLHttpRequestObject = false; 

       if(window.XMLHttpRequest) 
       { 
        if(typeof XMLHttpRequest != 'undefined') 
        { 
         try 
         { 
          XMLHttpRequestObject = new XMLHttpRequest(); 
         } 
         catch (e) 
         { 
          XMLHttpRequestObject = false; 
         } 
        } 
       } 
       else if (window.ActiveXObject) 
       { 
        try 
        { 
         XMLHttpRequestObject = new ActiveXObject('Msxml2.XMLHTTP'); 
        } 
        catch (e) 
        { 
         try 
         { 
          XMLHttpRequestObject = new ActiveXObject('Microsoft.XMLHTTP'); 
         } 
         catch (e) 
         { 
          XMLHttpRequestObject = false; 
         } 
        } 
       } 
       else 
       { 
        XMLHttpRequestObject = false; 
       } 
       return XMLHttpRequestObject; 
      } 

      //The Main Ajax Object 
      function AjaxRequest(p_RequestMethod, p_DestinationURL) 
      { 
       this.XMLHttpRequestObject = GetXMLHttpRequestObject(); 

       this.RequestedMethod = p_RequestMethod; 
       this.DestinationURL = p_DestinationURL; 

       this.XMLHttpRequestObject.open(this.RequestMethod, this.DestinationURL); 

       this.OnStateChange = function(Callback) 
       { 
        this.XMLHttpRequestObject.onreadystatechange = Callback; 
       } 

       this.Send = function(p_Content) 
       { 
        this.XMLHttpRequestObject.send(p_Content); 
       } 

       this.GetState() 
       { 
        return this.XMLHttpRequestObject.readyState; 
       } 

       this.GetResponseText = function() 
       { 
        return this.XMLHttpRequestObject.responseText; 
       } 

       this.GetResponseStatus = function() 
       { 
        return this.XMLHttpRequestObject.status; 
       } 

       this.GetResponseStatusText = function() 
       { 
        return this.XMLHttpRequestObject.statusText; 
       } 
      } 

      var Request; 

      function GetData() 
      { 
       Request = new AjaxRequest('POST', 'http://www.kalekold.net/ajax/Ajax.php'); 
       Request.OnStateChange = StateChange; 
       Request.Send(); 
      } 

      function StateChange() 
      { 
       window.alert("State: " + Request.GetState()); 
       window.alert("Response: " + Request.GetResponseStatus()); 
       window.alert("Response Text: " + Request.GetResponseStatusText()); 

       if(Request.GetState() == COMPLETE && Request.GetResponseStatus() == OK) 
       { 
        Result = Request.GetResponseText(); 
        window.alert(Result); 
       } 
      } 
     </script> 

    </head> 
    <body> 
     <form> 
      <textarea name="TextArea" rows="10" cols="80"></textarea><br /> 
      <input type="button" value="Load" onClick="GetData();"> 
     </form> 
    </body> 
</html> 

El PHP del archivo:

<?php 
$XML = <<< PROLOG 
<?xml version="1.0" encoding="iso-8859-1"?> 
PROLOG; 

$XML .= "<results>"; 
    $XML .= "<result>"; 
     $XML .= "<FirstName>Gary</FirstName>"; 
     $XML .= "<SecondName>Willoughby</SecondName>"; 
     $XML .= "<Age>35</Age>"; 
    $XML .= "</result>"; 
    $XML .= "<result>"; 
     $XML .= "<FirstName>Sara</FirstName>"; 
     $XML .= "<SecondName>Gostick</SecondName>"; 
     $XML .= "<Age>35</Age>"; 
    $XML .= "</result>"; 
$XML .= "</results>"; 

header("Content-Type: text/xml"); 
echo $XML; 
?> 

El error completo:

uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXMLHttpRequest.open]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: http://www.kalekold.net/ajax/ :: AjaxRequest :: line 63" data: no] 

Line 0 

simplemente no puedo ver donde va mal, alguna idea?

Respuesta

6

La excepción "Código de error devuelto por componente: 0x80070057 (NS_ERROR_ILLEGAL_VALUE)" es causado por un valor ilegal que se pasa a la llamada del método abierto.

Mirando a través de su código encontré falta de ortografía:

 
this.RequestedMethod = p_RequestMethod; 
this.DestinationURL = p_DestinationURL; 

this.XMLHttpRequestObject.open(this.RequestMethod, this.DestinationURL); 

propiedad Ver this.RequestedMethod establecido en p_RequestMethod y this.RequestMethod se pasa a la llamada del método "abierto".

Además, en lugar de crear su propio contenedor, recomendaría usar código abierto XMLHttpRequest.js - implementación de objetos XMLHttpRequest de navegador cruzado compatible con estándares, que también corrige unos 20 errores de implementaciones de objetos XMLHttpRequest nativos del navegador.

+0

Gracias, echaré un vistazo. Voy a usar otro marco, pero creo que sería mi experiencia de aprendizaje. El código anterior es solo un fragmento combinado de la fuente principal. Estaba cansado cuando busqué el problema. Gracias otra véz. –

+0

Entonces, ¿corrigió la falta de ortografía? –

+0

Sí, la ortografía era el problema. –

1

Este mensaje de error es uno de los 'caprichos' del objeto XMLHttpRequest de FireFox. El mismo problema en IE tendrá diferentes síntomas.

Usted no querrá lidiar con todas estas peculiaridades usted mismo ahora que hay muchas buenas bibliotecas por ahí.

Por ejemplo en llamadas a Netscape y FX XMLHttpRequestObject.responseText o XMLHttpRequestObject.status arroja un error "NS _..." por cualquier problema de conexión. IE devolverá el código de error de red del sistema operativo en su lugar, sin error. Si maneja esto usted mismo, tendrá que incorporar el manejo de errores para ambos.

Recomendaría jQuery. Prototype también es excelente.

+0

Uso jQuery ahora para Ajax. Es bastante impresionante. –

Cuestiones relacionadas