【发布时间】:2017-12-14 13:25:32
【问题描述】:
请问,如何将 fetch 的输出保存到变量中 - 以便能够像使用对象一样使用它?
代码如下:
var obj;
fetch("url", {
method: "POST",
body: JSON.stringify({
"filterParameters": {
"id": 12345678
}
}),
headers: {"content-type": "application/json"},
//credentials: 'include'
})
.then(res => res.json())
.then(console.log)
最后的console.log 将显示一个对象。但是当我试图将它保存到变量.then(res => obj = res.json()) 时,console.log(obj) 将不会保存对象,而是 Promise。
请教一下,如何把它变成一个保存在变量中的对象?
【问题讨论】:
标签: javascript fetch-api