Reputation: 151
I would like to get content from another website using JavaScript. I allready searched for code snippets and i found this http://www.w3schools.com/xml/tryit.asp?filename=try_dom_xmlhttprequest_responsetext I changed some values, that it would work for the website i use. When i ran this code the browser tells me that I was not allowed to get the content from this website, so i inserted: "header("Access-Control-Allow-Origin: http://....")" in the php section of the website and it worked but it displayed all the content of the page, now i would like to filter a div out of all the content and display only that div. Is there an easy way to do this using JS/Jquery?
Here is my code:
function loadXMLDoc(){
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("demo").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "http://.....", true);
xmlhttp.send();
}
I guess the problem is that i have to load the content from the other website first, and then run a piece of code that filters the content in the demo block.
Upvotes: 2
Views: 1395
Reputation: 26160
"Is there an easy way...to filter...."
Not "easy", per-se. What do you want to "filter"?
In order to edit / manipulate the information returned, where you have this:
document.getElementById("demo").innerHTML = xmlhttp.responseText;
You'd do something like this:
var content = xmlhttp.responseText;
// filter content with your code here;
document.getElementById("demo") = content;
NOTE: If you are using jQuery (your question includes the jQuery tag), then the code becomes simpler:
jQuery.ajax("http://www.urltoajax.com", {
success: function(content) {
// filter content with your code here
// Example:
content = $(content).find('my_elem').text();
// Then apply / append it to the element.
jQuery("#demo").html(content);
}
});
Upvotes: 1