Lee Blazek
Angular, JS, Mean Stack, Front-end, Node api's, with a side of iOS
- Jeeze... This guy is the Happy Gilmore of javascript + Web Development
This kind of functionality was previously achieved using XMLHttpRequest. Fetch provides a better alternative that can be easily used by other technologies such as Service Workers. Fetch also provides a single logical place to define other HTTP-related concepts such as CORS and extensions to HTTP.
- MDN
Text
// 1
fetch('data.json')
// 2
.then(response => {
// response is response object not the actual data
// extract content below
return response.json();
})
// 3
.then(data => {
//now do whatever with data
data.forEach(thing => console.log(thing))
});
const init = {
method: 'GET',
headers: {
customHeader: 1,
Accept: 'application/json',
Authorization: 'Bearer e49062f9-2fb8-405a-85d7-bdbba336d240',
},
mode: 'cors',
cache: 'default'
}
fetch('data.json', init)
.then(response => response.json())
.then(data => data.forEach(thing => console.log(thing)));
const init = {// ...}
fetch('data.json', init)
.then(response => {
// response.ok — seen in use above, this is a shorthand
// for checking that status is in the range 200-299 inclusive.
// This returns a Boolean.
if (Response.ok) {
return response.json()
}
throw new Error('Network response was not ok.');
})
.then(data => data.forEach(thing => console.log(thing)));- https://developer.mozilla.org/en-US/docs/Web/API/Response
Note that the fetch specification differs from jQuery.ajax() in mainly two ways that bear keeping in mind:
In a nut shell its an http request middle ware for the browser like middle ware in a node express server app.
Angular $http interceptors!!
-https://docs.angularjs.org/api/ng/service/$http
For purposes of global error handling, authentication, or any kind of synchronous or asynchronous pre-processing of request or postprocessing of responses, it is desirable to be able to intercept requests before they are handed to the server and responses before they are handed over to the application code that initiated these requests.
The interceptors leverage the promise APIs to fulfill this need for both synchronous and asynchronous pre-processing.
The interceptors are service factories that are registered with the $httpProvider by adding them to the $httpProvider.interceptors array. The factory is called and injected with dependencies (if specified) and returns the interceptor.
There are two kinds of interceptors (and two kinds of rejection interceptors):
1. request: interceptors get called with a http config object. The function is free to modify the config object or create a new one. The function needs to return the config object directly, or a promise containing the config or a new config object.
2. requestError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
3. response: interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.
4. responseError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
For purposes of global error handling, authentication, or any kind of synchronous or asynchronous pre-processing of request or postprocessing of responses, it is desirable to be able to intercept requests before they are handed to the server and responses before they are handed over to the application code that initiated these requests.
The interceptors leverage the promise APIs to fulfill this need for both synchronous and asynchronous pre-processing.
The interceptors are service factories that are registered with the $httpProvider by adding them to the $httpProvider.interceptors array. The factory is called and injected with dependencies (if specified) and returns the interceptor.
- https://docs.angularjs.org/api/ng/service/$http