How to read content from a website in javascript?

7

I would like to know how to read content from other web pages using just javascript or some library.

For example, from a remote news site, in the case: www.terra.com.br.

And I would like to create a webapp to read the latest news presented on this page, which does not have RSS and etc.

I know of the problems that could occur if the pages I'm feeding me change the layout or anything.

Would you have any way to do this?

    
asked by anonymous 03.03.2014 / 14:46

1 answer

10

You can directly make an AJAX request to the server, like this:

xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var html = xmlhttp.responseText;
        processPage(html);
    }
}

xmlhttp.open("GET", "http://www.terra.com.br/", true);
xmlhttp.send();

Note that depending on the server you may have problems with cross-origin requests. One simple way to circumvent this is to use a service like whateverorigin.org . In this case it looks like this:

xmlhttp.open("GET", "http://whateverorigin.org/get?url=" + 
                    encodeURIComponent("http://www.terra.com.br/"), true);
    
03.03.2014 / 15:12