Sha*_*tin 8 fetch typescript aurelia
我们有以下电话fetch.
this.http.fetch('flasher', { method: 'post', body: jsonPayload })
.then(response => response.json())
.then(data => console.log(data));
Run Code Online (Sandbox Code Playgroud)
当我们收到200响应但在收到500响应时没有记录到控制台时,这种方法有效.我们如何处理500?
Sha*_*tin 14
结合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);
});
Run Code Online (Sandbox Code Playgroud)
fetch()返回Promise包含Response对象的内容.本Promise可以成为既满足或拒绝.Fulfillment运行第一个then(),返回其承诺,并运行第二个then().拒绝抛出第一个then()并跳转到catch().