views:

82

answers:

4

i have script like this

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Testing Ajax</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<a class="test" href="getthis.php">click here</a>
<div class="get"></div>

<script type="text/javascript">
    $('.test').click(function(event){
        event.preventDefault();
        var a = $('body');
        $.ajax({
            url: "/getthis.php",
            dataType: 'text',
            success: function(data){            
                $('.get').append(data.find);
            }
        });
    });
</script>
</body>
</html>

with this script i try to get content getthis.php

getthis.php contains only this

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title></title>
    </head>
    <body>
        Olalalalala bebe
    </body>
    </html>

when i do this i get result the full html of the getthis.php

how i can to only get body content? which is mean only. "Olalalalala bebe"

can somebody give me explanation?

Thanks...

A: 

If not required you exclude html tags from your getthis.php have that data which you want to display on page.

Chinmayee
+3  A: 

use http://api.jquery.com/load/ you can load specific page fragments

jknair
+1 I could say this is the best approach based on the Op's problem
Reigel
can give example code which it's work?
GusDe CooL
$(".get").load("getthis.php" body); but if your response gethis.php page has a body tg with id ="thisbody" then the code will be $(".get").load("getthis.php" #thisbody);
jknair
+2  A: 

Wondering if this helps:

$(data).text();

This seems to work out of the box:

$.ajax({
    url: 'http://stackoverflow.com/questions/3861325/ajax-get-body-only/',
    success: function (data) {
        alert($(data).text().replace(/\s+/gm, ' '));
    }
});
Salman A
i get this in console.log "(an empty string)"
GusDe CooL
I've updated the post, now it seems to work in console. (PS: to run the example in console, you must run this from a browser window that is displaying a page from http://stackoverflow.com).
Salman A
+2  A: 

This should do it:

$('.get').load('/getthis.php body');

Edit: Sorry, I just tested this and it doesn't seem to work using body as a selector, which surprises me. If I change getthis.php to this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title></title>
    </head>
    <body>
        <div id="test">
        Olalalalala bebe
        </div>
    </body>
</html>

And call it like this:

$('.get').load('/getthis.php #test');

I get the result you are looking for. But it seems body is not a valid selector in this case.

Mark B
nope, the response still give the same result as my example.
GusDe CooL
yes you right. it's seems it didn't support body. i try with ID and Class it's work. thanks you very much.
GusDe CooL
You're welcome. And if anybody knows why this doesn't work with `body` as the selector, please comment and let us know!
Mark B