2

I have a web page is running on localhost. This web page has some JavaScript that runs when the page is loaded. I want to see if the web page can connect to the external internet.

I thought I would use Axios, to see if I could hit Google. So, I tried the following:

axios.get('https://www.google.com')
  .then(function (res) {
    alert('Google found!');
  })
  .catch(function (err) {
    console.log(err);
    alert('Cannot find Google');
  })
;

When this code runs, I see the following error in my console window:

XMLHttpRequest cannot load https://www.google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

My question is, how can I use Axios to see if I can reach the internet?

user687554
  • 10,663
  • 25
  • 77
  • 138
  • Short answer is you can't unless remote site accepts head requests, and you only make a head request. Use a proxy. – charlietfl Apr 22 '17 at 14:46

1 Answers1

0

Axios cannot make cross domain requests, You can try using a cross domain library. For example, jquery, zepto and so on.

  • Actually Axios can make cross domain requests, just need to include "crossDomain: true" in the request. See the answer here: https://stackoverflow.com/questions/42422494/cross-domain-at-axios – mczarnek Nov 13 '20 at 15:26
  • Axios can make cross-origin requests. You just have to enable CORS. – Renjith May 26 '21 at 04:04