我们有以下fetch
个电话.
this.http.fetch('flasher', { method: 'post', body: jsonPayload })
.then(response => response.json())
.then(data => console.log(data));
当我们收到200个响应时,这种方法有效,但当我们收到500个响应时,不会将任何内容记录到控制台.我们如何处理500美元?
我们有以下fetch
个电话.
this.http.fetch('flasher', { method: 'post', body: jsonPayload })
.then(response => response.json())
.then(data => console.log(data));
当我们收到200个响应时,这种方法有效,但当我们收到500个响应时,不会将任何内容记录到控制台.我们如何处理500美元?
结合then
和catch
个作品.
fetch('http://some-site.com/api/some.json')
.then(function(response) { // first then()
if(response.ok)
{
return response.text();
}
throw new Error('Something went wrong.');
})
.then(function(text) { // second then()
console.log('Request successful', text);
})
.catch(function(error) { // catch
console.log('Request failed', error);
});
fetch()
返回包含Response
对象的Promise
.Promise
可以被满足,也可以被拒绝.Fulfillment运行第一个then()
,返回其promise ,然后运行第二个then()
.拒绝投到前then()
,然后跳到catch()
.