-1

I wrote a JS program that sends arguments to a flask server and prints the returned value in the console. The server returns either [0] or [1] depending on the arguments sent.

However, when this program sends arguments to the server, the resultant fetch() data attribute is always an empty dict {} instead of [0] or [1]. Why is this? In Chrome DevTools, in the Network section, it shows the expected response, but this isn't reflected in fetch()'s data attribute

Code:

function predict(values) {
  fetch(`https://darrendube.pythonanywhere.com/?${stringify(values)}`)
.then((response) => JSON.stringify(response))
.then((data) => console.log(data))
}

What is printed in the console: enter image description here

What is shown in Chrome DevTools (and the value I expect): enter image description here

The fact that DevTools shows the expected response means that the arguments are correct and the server is working normally, so there must be a mistake in my code...

What could it be?

1 Answers1

4

The initial Response object does not directly contain the actual JSON response body but is instead a representation of the entire HTTP response. So, to extract the JSON body content from the Response object, we use the json() method.

   function predict(values) {
     fetch(`https://darrendube.pythonanywhere.com/?${stringify(values)}`)
   .then((response) => response.json())
   .then((data) => console.log(data))
    }
Greg Brodzik
  • 1,765
  • 7
  • 9