I'm teaching myself AJAX to AJAXify my site. In my template, I have the following JS code to get some JSON data from a view then append the data to a div.
function filter(type) {
$.getJSON(
'/activity_stream/global-activity-stream/',
{xhr: "true", filter: type},
function(data) {
$('.mainContent').children().remove();
$(data).appendTo('.mainContent');
});
}
$(".btn").click(function () {
filter("recent");
});
}
I think my view is returning proper JSON but now data is not being added to the .mainContent
div.
It gives this error:
Uncaught TypeError: Cannot read property 'ownerDocument' of undefined.
$('.mainContent').children().remove(); $(data).appendTo('.mainContent');
to$('.mainContent').html(data);
The problem is, when you doappendTo
, jquery expects the$(data)
to be a dom node, which it is most likely not in this case – karthikr$('.mainContent').html(data);
should show whatever came from the server (as just raw JSON text). (No need to remove the children before doing that.) You can use the browser debugging tools to check and see exactly what the server is returning. – Pointy