[英]Get the data from fetch -> promise -> response
I am trying to post some data to the server but I don't know how to get back the response data. 我试图将一些数据发布到服务器,但我不知道如何取回响应数据。
I have the following code: 我有以下代码:
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: login,
password: password,
})
}).then(function(a){
console.log(a);
})
It prints a Response
it contains data such as body (ReadableByteStream), bodyUsed (false), ok (true), status (200),... but I cannot find the data I get back, nowhere. 它打印一个
Response
它包含诸如body(ReadableByteStream),bodyUsed(false),ok(true),status(200)等数据,但是我找不到我回来的数据,无处可去。 When I open the chrome developer console - network I see the response data there. 当我打开chrome开发者控制台 - 网络时,我看到那里的响应数据。
What am I doing wrong? 我究竟做错了什么?
I've been looking for some resources how fetch, promises,... work but I couldn't find any well written. 我一直在寻找一些资源如何获取,承诺,...工作,但我找不到任何写得好。
There are further methods you call on a fetch response, such as .json()
, or .blob()
. 还有另外的方法,你在获取响应称,如
.json()
或.blob()
These methods return a promise which you can call .then()
on. 这些方法返回一个你可以调用
.then()
的promise。
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: login,
password: password,
})
})
.then(function (a) {
return a.json(); // call the json method on the response to get JSON
})
.then(function (json) {
console.log(json)
})
Check out some documentation on using fetch , and some other documentation on how the response object works in a fetch call. 查看有关使用fetch的一些文档,以及有关响应对象如何在fetch调用中工作的一些其他文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.