Update fetch()

This commit is contained in:
Rico Sta. Cruz 2016-06-03 19:34:43 +08:00
parent 4861e0ff2e
commit ee3f72b9d1
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 25 additions and 2 deletions

View File

@ -37,11 +37,34 @@ fetch('/data.json', {
headers: {
'Accept': 'application/json'
}
},
credentials: 'same-origin', // send cookies
credentials: 'include', // send cookies, even in CORS
})
```
##
### Catching errors
Non-2xx responses are still successful requests. Use another function to turn them to errors.
```js
fetch('/data.json')
.then(checkStatus)
```
```js
function checkStatus (res) {
if (res.status >= 200 && res.status < 300) {
return res
} else {
var err = new Error(response.statusText)
err.response = response
throw err
}
}
```
## Using with node.js