我正在使用Superagent在我的 React 应用程序中,我正在向 IPFS 调用一些电话API。具体来说,我正在将文件上传到我的 IPFS 服务器。现在,一切正常,当我上传一个或多个文件时,调用会通过并且文件显示在 IPFS 中没有问题。
当我上传多个文件时出现问题,响应似乎以纯文本形式返回,而不是 JSON,并且 superagent 抛出错误
client.js:399 Uncaught (in promise) Error: Parser is unable to parse the response
at Request.<anonymous> (client.js:399)
at Request.Emitter.emit (index.js:133)
at XMLHttpRequest.xhr.onreadystatechange (client.js:708)
需要明确的是,当上传单个文件时,我会得到一个很好的 JSON 响应,但是当我上传多个文件时,响应是纯文本格式的。
我可以强制 Superagent 返回响应并自行解析吗?或者我可以在调用时设置一些内容以便强制进行 json 解析吗?下面是我的 super 代理请求函数
add : acceptedFiles => {
const url = ipfs.getUrl("add")
const req = request.post(url)
acceptedFiles.forEach(file => req.attach(file.name, file))
req.then(res => {
return console.log(res);
})
}
最佳答案
我正在寻找一个更优雅的解决方案,但在找到它之前,我想提供我自己的解决方案。
我认为这个问题是由错误的响应Content-Type
设置引起的,但我还没有证实这个观点。
但是,您可以尝试以下操作:
req.catch(function (err) {
console.log(err.rawResponse)
})
至少,这解决了我的问题。
关于json - Superagent:错误:解析器无法解析响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46674687/