1

I am trying to download a webpage and extract the body.

Given I have the following code:

$.ajax({
    url: someAccessiblePublicUrlOnSameWebServer,
    dataType: 'html',
    success: function (data) {
        //data is correct at this point

        var body = $(data).find('body').html();

        //body is null. why ?
    }
});

success is called and data contains the expected html but body is always null. Why ?

Christof Jans
  • 1,854
  • 1
  • 17
  • 10
  • Is this your actual code? You're passing the empty `body` variable to `.find()`. –  Jan 11 '12 at 19:02
  • If `data` is an entire HTML document, you'll have issues trying to find the `` element in some browsers. –  Jan 11 '12 at 19:04
  • Possible duplicate of [jQuery .find("body").html() == null](https://stackoverflow.com/questions/7608159/jquery-findbody-html-null) – xhienne Mar 09 '19 at 01:52

5 Answers5

2
$.ajax({
    url: someAccessiblePublicUrl,
    dataType: 'html',
    success: function (data) {
        var body = $(data).find('body').html();

    }
});
redDevil
  • 1,909
  • 17
  • 25
1

It may be that you have a typo in there.

Did you mean:

var body = $(data).find('body').html();

Note the single ticks around body.

Dan
  • 1,003
  • 1
  • 7
  • 16
1

If you load HTML via Ajax call it will always return data as a string so you will be unable to apply normal jQuery selectors to a response. If you convert the data to $(data) you will also not be able to access body as $(data) is a collection of the contents of the body (stripped by jQuery internal clean() method). You have a few options depending on what you want to do with the result:

If you want to just append the body of the loaded html somewhere in the document you can do this:

$.get('http://your_url', function(data) {

                    $('.result').html(data);

                });

This will load just the body contents to the .result container. If you want to do any further processing you can access the selectors from there.

If you just want to manipulate the unattached fragment you can access it's elements by using filter & get.

$(data).filter('p').get() //will get all para DOM nodes
$($(data).filter("#test2").get()).text() //will get text of one specific dom node

Another option if you want to process the data in the body it might be faster to process it as XML - for XML processing look at http://think2loud.com/224-reading-xml-with-jquery/. Using your example it would be something like that:

 $.ajax({
                        url : "http://mypage",
                        dataType : 'xml', //change dataType to XML
                        success : function(data) {
                            //data is correct at this point
                            $(data).find('html').each(function() {
//here you can find whatever you want                               
a = $(this).find("body")
                                console.log(a);

                            })
                        }
                    })
Michal
  • 13,439
  • 3
  • 35
  • 33
0

Unless it was a typo, you need quotes around the "body" inside your find method.

IE: var body = $(data).find('body').html();

That could be your problem.

B. Notess
  • 525
  • 1
  • 6
  • 11
  • fixed the type. still having same problem. – Christof Jans Jan 11 '12 at 19:09
  • @ChristofJans Weird. I see the issue. I found a workaround where I use var contents = $(data).contents() var body = contents[1], of course this depends on the structure of your page xhtml. – B. Notess Jan 11 '12 at 20:24
0

Correction to my last response.

Check out this thread parse html string with jquery

Using that, I think this will work

var body = $("body", $(data)).html();

Community
  • 1
  • 1
John F.
  • 4,780
  • 5
  • 28
  • 40