Cargando fragmentos XHTML sobre AJAX con jQuery
Pregunta
Estoy intentando cargar fragmentos de marcado XHTML usando jQuery. $.fn.load
función, pero genera un error al intentar agregar el nuevo marcado al DOM.He reducido esto a la declaración XML (<?xml...?>
) - la vista funciona si devuelvo texto estático sin la declaración.No entiendo por qué esto causaría una falla, o si la culpa es de jQuery, Firefox o mi código.
¿Cómo debo insertar fragmentos XHTML en el DOM usando jQuery?
Usar $.get no funciona: la devolución de llamada recibe un Document
objeto, y cuando intento insertarlo en el DOM, recibo el siguiente error:
uncaught exception: Node cannot be inserted at the specified point in the hierarchy (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)
http://localhost:8000/static/1222832186/pixra/script/jquery-1.2.6.js
Line 257
Este es mi código:
$body = $("#div-to-fill");
$.get ("/testfile.xhtml", undefined, function (data)
{
console.debug ("data = %o", data); // data = Document
$body.children ().replaceWith (data); // error
}, 'xml');
Una respuesta de muestra:
<?xml version="1.0" encoding="UTF-8"?>
<div xmlns="http://www.w3.org/1999/xhtml">
<form action="/gallery/image/edit-description/11529/" method="post">
<div>content here</div>
</form>
</div>
Solución
Pruebe esto en su lugar (acabo de hacer una prueba rápida y parece funcionar):
$body = $("#div-to-fill");
$.get ("/testfile.xhtml", function (data)
{
$body.html($(data).children());
}, 'xml');
Básicamente, .children () obtendrá el nodo raíz y reemplazará el contenido de su div con él. Supongo que no puede insertar exactamente un documento xml con la declaración & Lt;? Xml en el DOM ...