How to call and get via GET an external HTML page using Axios?

Asked

Viewed 269 times

1

I am trying to get any external html page via an Axios.get(), passing the url of the page in question as parameter:

axios.get("http://www.google.com")
        .then(resp => {
            retorno = resp.body
         })

Only the server of these external pages denies access and returns the following in my application browser:

Access to Xmlhttprequest at 'https://www.google.com/' (redirected from 'http://www.google.com/') from origin 'http://localhost:8081' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested Resource.

I do not know if it is possible to do so. If not, there is another way?

2 answers

2


The reason is security. Browsers do not allow scripts to request content from pages with a different domain than the one they are running. (and. g: your script is being run on www.suaurl.com.br and from that url it requests content from the www.google.com page), unless the target site (www.google.com) enables CORS and allows the site that runs your script (www.suaurl.com.br) to order. (see in: https://developer.mozilla.org/en-US/docs/Web/HTTP/Controle_Acesso_CORS). In the case of the above friend it worked because it is probably running the code in the backend (using Node) .

  • 1

    So if I run this in the back-end with Node it should work?

1

I tested your code and could not reproduce this error. However, if you want the html page you have to use resp.data and not resp.body.

If you don’t find a solution, I suggest you try the request.

Ex:

const request = require('request');
 request('http://www.google.com', function (error, response, body) {
   retorno = body;
});
  • 1

    Poise, that bizarre. I’m trying to get the Google main page even.

  • But I’ll try the require for a moment

  • HTML comes in the body variable of callbacak, but I can’t use its value outside of this function: https://answall.com/questions/452761/obten-html-de-url-externa-usandom%C3%b3dulo-request-do-Node

Browser other questions tagged

You are not signed in. Login or sign up in order to post.