Get data from a sample & # 8594; promise & # 8594; answer

I am trying to send some data to the server, but I do not know how to return the response data.

I have the following code:

fetch(url, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ email: login, password: password, }) }).then(function(a){ console.log(a); }) 

It prints a Response , it contains data such as body (ReadableByteStream), bodyUsed (false), ok (true), status (200), ... but I can not find the data that I will return anywhere. When I open the Chrome Developer Console, I see the response data there.

What am I doing wrong?

I was looking for some resources like fetch, promises, ... work, but I could not find a well written one.

+5
source share
1 answer

There are additional methods that you call to respond to a selection, such as .json() or .blob() . These methods return a promise that you can call .then() on.

 fetch(url, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ email: login, password: password, }) }) .then(function (a) { return a.json(); // call the json method on the response to get JSON }) .then(function (json) { console.log(json) }) 

Check out the documentation on using fetch and other documentation on how the sampling call response object works.

+11
source

All Articles