Why can't I parse the Ajax html GET response in jQuery?

I temper with the Chrome extension, where I use an Ajax request to retrieve HTML code from the requested URL. This works, but I want to get all the text values ​​for some specific elements. For example, everything with the script.js class.heading-bold

  

$.ajax({
        url: "http://page.com/page.html",
        type: "GET",
        dataType: "html",
          success: function(data) {
              console.log($(data).filter( '.heading_bold' ).text()); 
          }
    });

HTML response

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>Beerpong</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Expires" content="0" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    </head>
     <body>
        <div id="table-container">
            <table>
               <tbody>
                  <tr>
                   <td><div class="heading_bold">Beerpong</div></td>
                  </tr>
                </tbody>
            </table>
        </div>
     </body>
    </html>

Logging in to the console works fine. This is my conclusion:

Uncaught Error: Syntax error, unrecognized expression: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0....

Why? Why is this not just console.log my desired values?

+4
source share
2 answers

If you are using jquery 1.9, follow these steps:

...
success: function(data) {
   var html = $.parseHTML(data); 
   console.log($(html).find( '.heading_bold' ).text()); 
}
..

jQuery 1.9:: HTML-, jQuery(), - , , . , " ", Sizzle. jQuery.parseHTML() HTML.

+16

, "load" "$.get()", DOM.

$("#result").load("page.html .heading_bold",function(response){
console.log($(this).find(".heading_bold").val());           
});

, .

+1

All Articles