使用Axios作为Ping

时间:2017-04-22 14:31:22

标签: javascript axios

我有一个在localhost上运行的网页。此网页包含一些在加载页面时运行的JavaScript。我想看看网页是否可以连接到外部互联网。

我以为我会使用Axios来查看我是否可以访问Google。所以,我尝试了以下内容:

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

当此代码运行时,我在控制台窗口中看到以下错误:

XMLHttpRequest无法加载https://www.google.com/。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:3000”访问。

我的问题是,如何使用Axios查看我是否可以上网?

1 个答案:

答案 0 :(得分:0)

Axios无法发出跨域请求,您可以尝试使用跨域库。 例如,jquery,zepto等。