fetch to get data from server 1

fetch to get data from server

fetch(url).then(function(response) {
  response.text().then(function(dataFromServer) {
    //do stuff
  });
});

Here is what the above code is Doing:
1. We create a new Request object with the URL we want to fetch.
2. We call fetch() on the request object.
3. We get a response object from the server.
4. We call response.text() to get the data from the server as a string.
5. We call .then() on the promise returned by response.text().
6. We pass a callback function to .then(). This callback function will be called when the data is ready.
7. We get the data from the server as a string and do stuff with it.

Similar Posts