Reputation: 1557
I'm trying to get the HTML from another domain (Google for the example). I use this library, and I can get the HTML with it:
jQuery(document).ready(function($) {
$.ajax({
url: 'http://google.fr',
type: 'GET',
success: function(res) {
$html = $(res.responseText);
}
});
});
The string returned by res.responseText
contains all the HTML, but when I do $(res.responseText)
, there isn't all the tags, for example I can't get the title by doing $(res.responseText).find("title")
. I even tried the function $.parseHTML
, I have the same result. Why is my HTML not parsed correctly?
Upvotes: 2
Views: 221
Reputation: 167172
Everything works as expected, but let's create a fake element:
$html = $("<div />", {html: res.responseText});
$html.find("title"); // This would work.
Upvotes: 1
Reputation: 6538
Can you try something like this ?
https://jsfiddle.net/omsmvksg/
jQuery(document).ready(function($) {
$.ajax({
url: 'https://google.fr',
type: 'GET',
dataType : 'html',
success: function(res) {
console.log(res.responseText);
$html = $(res.responseText);
console.log($html.find("._yKg").length);
}
});
});
It logs the good count of _yKg
class in the html downloaded.
Upvotes: 1