无法使用Axios或D3-fetch解决Dropbox.com重定向

时间:2019-11-06 01:18:35

标签: redirect https axios fetch dropbox

我正在尝试从Dropbox链接中获取浏览器中的数据:https://www.dropbox.com/s/101qhfi454zba9n/test.txt?dl=1

使用Axios可以在Node中运行:

axios.get('https://www.dropbox.com/s/101qhfi454zba9n/test.txt?dl=1').then(x => {
  console.log('Received correctly')
  console.log(x.data);
}).catch(error => {
    console.log('Error', error.message);
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log(error.request);      
    }
    console.log(error) 
});

https://runkit.com/stevebennett/5dc21d04bbc625001a38699b

但是,它在浏览器中不起作用(单击“控制台”选项卡):

https://codepen.io/stevebennett/pen/QWWmaBy

我刚刚得到一个通用的“网络错误”。我看到正在检索301重定向,这看起来很正常,但是由于某种原因,它没有被执行。

与D3完全相同:“尝试获取资源时出现NetworkError。”

https://codepen.io/stevebennett/pen/KKKoZYN

这是怎么回事?

1 个答案:

答案 0 :(得分:1)

由于CORS策略,这似乎失败了。直接用XMLHttpRequest尝试此操作失败:

  

从原点“ ...”对“ https://www.dropbox.com/s/101qhfi454zba9n/test.txt?dl=1”处XMLHttpRequest的访问已被CORS策略阻止:请求的资源上没有“ Access-Control-Allow-Origin”标头。

www.dropbox.com本身不允许使用CORS。