我怎样才能从a处获取信息ReadableStream
object?
我正在使用 Fetch API,但文档中没有明确说明这一点。
尸体正在被归还ReadableStream
我只想访问此流中的属性。在浏览器开发工具的 Response 下,我似乎已将此信息以 JavaScript 对象的形式组织成属性。
fetch('http://192.168.5.6:2000/api/car', obj)
.then((res) => {
if(!res.ok) {
console.log("Failure:" + res.statusText);
throw new Error('HTTP ' + res.status);
} else {
console.log("Success :" + res.statusText);
return res.body // what gives?
}
})
为了从a访问数据ReadableStream
您需要调用其中一种转换方法(可用文档here).
举个例子:
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(function(response) {
// The response is a Response instance.
// You parse the data into a useable format using `.json()`
return response.json();
}).then(function(data) {
// `data` is the parsed version of the JSON returned from the above endpoint.
console.log(data); // { "userId": 1, "id": 1, "title": "...", "body": "..." }
});
EDIT:如果您的数据返回类型不是 JSON 或者您不需要 JSON,则使用text()
举个例子:
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(function(response) {
return response.text();
}).then(function(data) {
console.log(data); // this will be a string
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)