Update fetch()
This commit is contained in:
parent
4861e0ff2e
commit
ee3f72b9d1
27
js-fetch.md
27
js-fetch.md
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue