- handling cookies
fetch('https://example.com', { credentials: 'include' })
- HTTP status codes fetch('https://example.com').then(response => {
if (response.ok) {
// Response is 200-299
}
if (response.status === 404) {
// Status code specific handling
}
});
- CORS fetch('https://example.com', { mode: 'no-cors' });
- JSON handling fetch('https://example.com').then(response => response.json()).then(json => {
// Do something with a parsed JSON object
});
I've used all of the above patterns regularly for several years now and never found any of them particularly cumbersome and certainly not lacking feature wise. If async/await syntax is available to you, it's even more succinct than the Promise style above.With that said, I've not used $.ajax in anger for a good long while so I may be missing out, particularly as I note there have been API changes in newer versions of jQuery. Are there some specific use cases that you've found fetch to be particularly inept in dealing with?