Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.ajax() get body only

Tags:

html

jquery

ajax

i have script like this

<html xmlns="http://www.w3.org/1999/xhtml">
<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">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <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...

like image 795
GusDeCooL Avatar asked Oct 05 '10 06:10

GusDeCooL


2 Answers

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

like image 147
jknair Avatar answered Oct 04 '22 20:10

jknair


Pretty easy. Wrap inner of BODY by one more layer of DIV will do:

$.get(url, {}, function(data){
  var data = data.replace('<body', '<body><div id="body"').replace('</body>','</div></body>');
  var body = $(data).filter('#body');
  //... do what you want with body ...
});
like image 20
user1709374 Avatar answered Oct 04 '22 21:10

user1709374