【发布时间】:2016-11-02 21:17:08
【问题描述】:
我正在使用新的 Fetch API 从 API 中检索对象。这是我的代码。
getUserYguid(){
fetch(this.myapi, {
credentials: "include",
method: 'get',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: null
}).then(function(response){
console.log(response.status)
console.log(response.json());
let obj = response.text();
console.log(obj.name);
}).catch(function(error){
console.log('Request failed', error);
});
}
当我调用 response.status 时,它工作正常,我可以看到状态消息为 200。 当我调用 response.json() 或 response.text() 时,我可以看到返回的完整对象。 问题是下一行代码不起作用。 当我尝试从对象中检索属性时,例如
console.log(obj.name);
我明白了,
undefined
【问题讨论】: