簡體   English   中英

由於 CORS 問題,通過瀏覽器的 JS-IPFS http 客戶端失敗

[英]JS-IPFS http client via browser fails due to CORS issue

我做了一些研究和試驗基礎上,JS-IPF問題-HTTP客戶端瀏覽器例如這里

當我嘗試從 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 "[\"*\"]"

但即使這似乎也不起作用。 我開始研究可能使用這里提到的自定義標頭

沒運氣。

我像這樣設置 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\"]"

希望這能解決你的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM