Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find body tag in an ajax HTML response

Tags:

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'); 
like image 599
Youss Avatar asked Jan 20 '13 09:01

Youss


People also ask

What is the tag for body in HTML?

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.

What is response type in Ajax?

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.

What does body mean in Javascript?

body in javascript is a direct reference to the DOM element representing the <body> portion of the page.


1 Answers

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".)

like image 125
Yush0 Avatar answered Oct 14 '22 06:10

Yush0