【问题标题】:Can't resolve Dropbox.com redirects with Axios or D3-fetch无法使用 Axios 或 D3-fetch 解析 Dropbox.com 重定向
【发布时间】:2019-11-06 01:18:35
【问题描述】:

我正在尝试从 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 完全相同:“尝试获取资源时发生网络错误。”

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

发生了什么事?

【问题讨论】:

    标签: redirect https axios fetch dropbox


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗯,所以没有办法直接从浏览器访问 Dropbox 托管的文件?
    • Dropbox 确实提供了an actual API 用于上传和下载文件以及其他操作,但这需要更多的工作来实现。从技术上讲,看起来使用旧的 dl.dropboxusercontent.com 主机名(例如,像这样:'https://dl.dropboxusercontent.com/s/101qhfi454zba9n/test.txt')仍然有效,但这不受官方支持,如有更改,恕不另行通知。
    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 2021-10-12
    • 2020-10-15
    • 1970-01-01
    • 2017-12-29
    • 2021-06-02
    • 2019-08-03
    • 1970-01-01
    相关资源
    最近更新 更多