【问题标题】:JS-IPFS http client via browser fails due to CORS issue由于 CORS 问题,通过浏览器的 JS-IPFS http 客户端失败
【发布时间】:2019-08-29 09:34:30
【问题描述】:

我正在根据js-ipfs-http-client浏览器示例here做一些研究和测试

当我尝试从 IPFS 获得响应时,我从 Firefox 控制台收到以下警告:

跨域请求被阻止:同源策略不允许读取位于http://localhost:5001/api/v0/add?wrapWithDirectory=true&progress=true&wrap-with-directory=true&stream-channels=true 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。

我已经尝试了建议的(肮脏)修复,您可以从终端更改 IPFS 配置:

ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"

但即使这样似乎也不起作用。我开始研究可能使用提到的自定义标头here

运气不好。

我这样设置 IPFS: const ipfsClient = require('ipfs-http-client'); var ipfs = ipfsClient('localhost', '5001');

然后,一旦我从用户那里获得了一些文件,我就会尝试将其保存到 IPFS,如下所示:

function saveToIpfsWithFilename (file) 
{
    console.log('running save');

    let ipfsId;
    const fileStream = fileReaderPullStream(file);
    const fileDetails = 
    {
      path: file.name,
      content: fileStream
    };

    const options =
    {
      wrapWithDirectory: true,
      progress: (prog) => console.log(`received: ${prog}`)
    };

    ipfs.add(fileDetails, options).then((response) => 
    {
        console.log(response)
        // CID of wrapping directory is returned last
        ipfsId = response[response.length - 1].hash
        console.log(ipfsId)
    }).catch((err) => {
        console.error(err)
      });
}

【问题讨论】:

标签: javascript node.js browserify ipfs


【解决方案1】:

我也遇到了同样的问题,然后我尝试运行以下这些命令,这对我有用

ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080
ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"

希望这能解决您的问题

【讨论】:

    猜你喜欢
    • 2011-03-13
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2016-11-15
    相关资源
    最近更新 更多