Javascript 如何通过fetch在单个回调中获取json和响应状态?
我想在一次回调中获得json内容、状态和其他响应属性。除了示例代码外,我不能再深入了:Javascript 如何通过fetch在单个回调中获取json和响应状态?,javascript,Javascript,我想在一次回调中获得json内容、状态和其他响应属性。除了示例代码外,我不能再深入了: fetch('/users.json') .then(function(response) { console.log(response.status) return response.json() }).then(function(json) { console.log('parsed json', json) }) 我可以打印状态,但是在第一次回调中尝试返回响应状态和
fetch('/users.json')
.then(function(response) {
console.log(response.status)
return response.json()
}).then(function(json) {
console.log('parsed json', json)
})
我可以打印状态,但是在第一次回调中尝试返回响应状态和json会导致第二次回调得到承诺,我认为这是第一次
如何在一个回调中获取json以及其他属性?试试以下方法:
.fetch('/users.json')
.then(function(response) {
return {
status: response.status,
data: response.json()
};
}).then(function(result) {
console.log('parsed json', result.data);
console.log('status', result.status);
});
还是得到了承诺对象