Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery append get result fails for XHTML

When I try to append() the XHTML result of a $.get() to a container element,

$.get("my-webservice", function(data){
    $("#some-container").append(data);
  });

I get an error:

TypeError: 'null' is not an object (evaluating 'e.ownerDocument')

I also tried:

$("#some-container").append($(data));

and get the same error. load() works fine, so I know the webservice is returning good data:

$("#some-container").load("my-webservice");

At the webservice end, if I encode the XHTML as a string before returning to $.get(), everything works as expected. The append() docs say it accepts a string, DOM Element, or jQuery object - I suspect I am getting something about that wrong. Any suggestions?

The webservice is just returning some simple bootstrap XHTML:

<div class="row">
  <div class="span5">Div stuff</div>
  <div class="span4">More div stuff</div>
</div>
like image 346
wst Avatar asked Dec 05 '25 15:12

wst


1 Answers

jQuery is trying to make its best guess as to the type of document being returned when you use the $.get function without specifying a dataType. As the last parameter of the $.get function try adding 'html' to specify the dataType. See the jQuery documentation for more information.

$.get("my-webservice", function(data){
    $("#some-container").append(data);
}, 'html');
like image 168
Jacob VanScoy Avatar answered Dec 07 '25 06:12

Jacob VanScoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!