gpt4 book ai didi

javascript - 由于 CORS 问题,通过浏览器的 JS-IPFS http 客户端失败

转载 作者:行者123 更新时间:2023-12-05 06:27:05 28 4
gpt4 key购买 nike

我正在根据 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 header “Access-Control-Allow-Origin”)。

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

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

但即使这样似乎也行不通。我开始研究可能使用提到的自定义 header 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)
});
}

最佳答案

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

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\"]"

希望这能解决你的问题

关于javascript - 由于 CORS 问题,通过浏览器的 JS-IPFS http 客户端失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569973/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com