I'm making an ajax call to fetch content and append this content like this:
$(function(){ var site = $('input').val(); $.get('file.php', { site:site }, function(data){ mas = $(data).find('a'); mas.map(function(elem, index) { divs = $(this).html(); $('#result').append('' + divs + ''); }) }, 'html'); });
The problem is that when I change a
in body
I get nothing (no error, just no html). Im assuming body is a tag just like 'a' is? What am I doing wrong?
So this works for me:
mas = $(data).find('a');
But this doesn't:
mas = $(data).find('body');
Definition and Usage. The <body> tag defines the document's body. The <body> element contains all the contents of an HTML document, such as headings, paragraphs, images, hyperlinks, tables, lists, etc.
Response is the object passed as the first argument of all Ajax requests callbacks. This is a wrapper around the native xmlHttpRequest object. It normalizes cross-browser issues while adding support for JSON via the responseJSON and headerJSON properties.
body in javascript is a direct reference to the DOM element representing the <body> portion of the page.
I ended up with this simple solution:
var body = data.substring(data.indexOf("<body>")+6,data.indexOf("</body>")); $('body').html(body);
Works also with head or any other tag.
(A solution with xml parsing would be nicer but with an invalid XML response you have to do some "string parsing".)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With